And*_*icz 5 asp.net iis iis-7.5 iis-express asp.net-core
我正在关注[1],用于设置我的第一个.NET Core WebAPI(从“旧的” WebAPI移出)。当我执行HttpPost操作时,如教程中所示:
[HttpPost]
public IActionResult Create([FromBody] TodoItem item)
{
if (item == null)
{
return BadRequest();
}
TodoItems.Add(item);
return CreatedAtAction("GetTodo", new { id = item.Key }, item);
}
Run Code Online (Sandbox Code Playgroud)
退出控制器后,我收到HttpError 500。CreatedAtAction如我期望的那样,返回状态代码为201的正确对象,并且在我的方法退出服务器后以某种方式将其变为500。我似乎无法进入其余的管道。跟踪给我以下错误:
81. -GENERAL_REQUEST_ENTITY
82. -NOTIFY_MODULE_COMPLETION
83. -MODULE_SET_RESPONSE_ERROR_STATUS [Warning] 171ms
ModuleName AspNetCoreModule
Notification EXECUTE_REQUEST_HANDLER
HttpStatus 500
HttpReason Internal Server Error
HttpSubStatus 0
ErrorCode The operation completed successfully. (0x0)
ConfigExceptionInfo
Run Code Online (Sandbox Code Playgroud)
一切(Program.cs,Startup.cs)的设置均与[1]中的设置完全相同。在IISExpress(VS2015 Update 3)和IIS7.5(Windows 7)中的行为完全相同。其他返回类型,200(new ObjectResult(item)用于GET,404用于PUT NotFound()或204 NoContentResult()用于PUT都可以)。所以似乎以某种方式遇到了201的问题。有什么想法吗?
[1] https://docs.asp.net/zh/latest/tutorials/first-web-api.html
更新 每个请求发布其他详细信息:
Startup.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FirstWebApi.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace FirstWebApi
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddLogging();
services.AddSingleton<ITodoRepository, TodoRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
app.UseMvc();
}
}
}
Run Code Online (Sandbox Code Playgroud)
TodoController.cs:
using System.Collections.Generic;
using FirstWebApi.Models;
using Microsoft.AspNetCore.Mvc;
namespace FirstWebApi.Controllers
{
[Route("api/[controller]")]
public class TodoController : Controller
{
public TodoController(ITodoRepository todoItems)
{
TodoItems = todoItems;
}
public ITodoRepository TodoItems { get; set; }
public IEnumerable<TodoItem> GetAll()
{
return TodoItems.GetAll();
}
[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(string id)
{
var item = TodoItems.Find(id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
}
[HttpPost]
public IActionResult Create([FromBody] TodoItem item)
{
if (item == null)
{
return BadRequest();
}
TodoItems.Add(item);
return CreatedAtAction("GetTodo", new { id = item.Key }, item);
}
[HttpPut("{id}")]
public IActionResult Update(string id, [FromBody] TodoItem item)
{
if (item == null || item.Key != id)
{
return BadRequest();
}
var todo = TodoItems.Find(id);
if (todo == null)
{
return NotFound();
}
TodoItems.Update(item);
return new NoContentResult();
}
[HttpDelete("{id}")]
public void Delete(string id)
{
TodoItems.Remove(id);
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新2
原来,CreateAtAction有201次尝试重定向到未处理的路由:
System.InvalidOperationException: No route matches the supplied values.
at Microsoft.AspNetCore.Mvc.CreatedAtActionResult.OnFormatting(ActionContext context)
Run Code Online (Sandbox Code Playgroud)
仍然不确定为什么,因为该方法GetById应映射到GetTodo每个Controller设置。
因此,经过一些调试后,我找到了造成问题的原因。事实证明,this.Url.Action("Get", "GetTodo", new { id=item.Key })它将返回null,而500则来自IIS无法匹配附加到201的路由的事实。
事实证明,您要么需要设置:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}");
});
Run Code Online (Sandbox Code Playgroud)
或使用CreateAtRoute。
| 归档时间: |
|
| 查看次数: |
3691 次 |
| 最近记录: |