API ASP.NET 5“POST”成功,但出现错误“没有路由与提供的值匹配”
Posted
技术标签:
【中文标题】API ASP.NET 5“POST”成功,但出现错误“没有路由与提供的值匹配”【英文标题】:API ASP.NET 5 "POST" successfully, although with the error "No route matches the supplied values" 【发布时间】:2021-12-08 06:21:03 【问题描述】:尽管它看起来像一个重复的问题,但 *** 的答案似乎都不适合或解决我的问题。
基本上,我正在使用 ASP.NET 5 制作一个 REST API,其中包含两个实体:“Server”和“Video”,实体“Video”依赖于实体“Server”。
GET、POST、PUT、DELETE...方法都与“服务器”实体一起工作;问题是当我尝试在“视频”实体上使用 POST 时。
System.InvalidOperationException: No route matches the supplied values.
at Microsoft.AspNetCore.Mvc.CreatedAtActionResult.OnFormatting(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsyncCore(ActionContext context, ObjectResult result, Type objectType, Object value)
at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsync(ActionContext context, ObjectResult result)
at Microsoft.AspNetCore.Mvc.ObjectResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultAsync(IActionResult result)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
有趣的是,即使它返回错误,当我使用 GET 进行检查时,JSON 也会成功发布。
这是一段 VideosController 的代码:
namespace SeventhApi.Controllers
[Route("api/servers/serverId/videos")]
[ApiController]
public class VideosController : ControllerBase
private readonly VideoContext _context;
public VideosController(VideoContext context)
_context = context;
[HttpGet("id")]
public async Task<ActionResult<Video>> GetVideo(Guid id)
var video = await _context.Videos.FindAsync(id);
if (video == null)
return NotFound();
return video;
[HttpPost]
public async Task<ActionResult<Video>> PostVideo(Video video)
_context.Videos.Add(video);
await _context.SaveChangesAsync();
return CreatedAtAction("GetVideo", new id = video.VideoId , video);
还有一大块启动:
public void ConfigureServices(IServiceCollection services)
services.AddControllers();
services.AddDbContext<ServerContext>(opt => opt.UseInMemoryDatabase("Server"));
services.AddDbContext<VideoContext>(opt => opt.UseInMemoryDatabase("Video"));
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
endpoints.MapControllers();
);
根据我在这里找到的答案,我倾向于认为这是一些路由错误,但没有一个答案能解决我的问题。
【问题讨论】:
你确定“GetVideo”方法存在吗?更喜欢使用nameof(GetVideo)
路由控制器是:“api/servers/serverId/videos”。你不应该提供 serverId 参数吗?
【参考方案1】:
url有两个参数,serverId id。
return CreatedAtAction("GetVideo", new id = video.VideoId, serverId = 1 , video);
【讨论】:
【参考方案2】:正如 Hazrelle 所说,这实际上是使用 nameof(GetVideo) 的问题:
[HttpPost]
[ActionName(nameof(GetVideo))]
public async Task<ActionResult<Video>> PostVideo(Video video)
_context.Videos.Add(video);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetVideo), new id = video.VideoId , video);
【讨论】:
以上是关于API ASP.NET 5“POST”成功,但出现错误“没有路由与提供的值匹配”的主要内容,如果未能解决你的问题,请参考以下文章
400 仅在启用 cors 时响应 asp.net web api POST 请求
在 ASP.NET MVC 5 控制器中使用 POST 从 dotnet Core Web API 下载文件
为啥我的 Asp.Net Core 日志中出现“不支持 POST 请求”?