ASP.NET CORE、Web API:没有路由匹配提供的值 [英] ASP.NET CORE, Web API: No route matches the supplied values

查看:29
本文介绍了ASP.NET CORE、Web API:没有路由匹配提供的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:这个问题是在 2016 年提出的.这个问题的原始答案是更新 microsoft api versiong 包.目前,该问题再次出现,但出于其他原因.

原始问题:

我在 asp.net core (web api) 中的路由有一些问题.

i have some problems with the routing in asp.net core (web api).

我有这个控制器(简化版):

I have this Controller (simplified):

[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[Controller]")]
public class DocumentController : Controller
{

[HttpGet("{guid}", Name = "GetDocument")]
public IActionResult GetByGuid(string guid)
{
    var doc = DocumentDataProvider.Find(guid);
    if (doc == null)
        return NotFound();

    return new ObjectResult(doc) {StatusCode = 200};
}


[HttpPost]
public IActionResult Create([FromBody] Document doc)
{
    //... Creating Doc

    // Does not work   
    var val = CreatedAtRoute("GetDocument", new {guid = doc.Guid.ToString("N")}, document);

    // or this: 
    CreatedAtRoute("GetDocument", new { controller = "Document", guid = doc.Guid.ToString("N")}, document);
    // neither this
    var val = CreatedAtRoute("GetDocument", new { version = "1", controller = "Document", guid = doc.Guid.ToString("N")}, document);

    return val;
}
}

如果我调用 Create,文档被创建,路由对象被创建,但我收到错误没有路由与提供的值匹配"并获得 500 状态.

If i call Create, the document is created and the routing object was created but i get the error "No route matches the supplied values" and get a 500 status.

我可以直接调用 GetByGuid,没有任何问题.

I can call the GetByGuid directly, without any problems.

我找不到任何有关 asp.net 核心的调试帮助(如任何现有的路由调试器).

I couldn't find any debugging help for asp.net core (like any existing routing debugger).

我将不胜感激!

编辑看起来这将是微软版本控制包中的一个错误..如果我定义了修复路由/api/v1/[Controller] 它正在工作.

EDIT Looks like it would be a bug from microsoft's versioning package.. if i define the fix route /api/v1/[Controller] it's working.

但这对我来说不是解决方案.

But that's not a solution for me.

推荐答案

我来回答我自己的问题:这确实是微软版本控制包中的一个错误,很快就会修复.

I'll answer my own question: It was really a bug in the versioning package of microsoft and it's gonna be fixed soon.

https://github.com/Microsoft/aspnet-api-versioning/问题/18

这篇关于ASP.NET CORE、Web API:没有路由匹配提供的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆