ASP.NET Core"CreatedAtRoute"失败 [英] ASP.NET Core "CreatedAtRoute" Failure

查看:112
本文介绍了ASP.NET Core"CreatedAtRoute"失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET Core应用程序内部,我有一个像这样的控制器动作:

Inside my ASP.NET Core app I have a controller action like this:

[HttpPost]
public async Task<IActionResult> CreateSubscriber([FromBody] SubscriberDef subscriber)
{
    //...implementation removed

    var link = Url.Link("SubscriberLink", new { id = subscriber.ID });
        return Created(link, null);
}

上面的代码按预期工作.但是,如果我使用内置方法"CreatedAtRoute",则会出现异常:

The above code works as expected. However, if I use the built-in method "CreatedAtRoute", then I get an exception:

[HttpPost]
public async Task<IActionResult> CreateSubscriber([FromBody] SubscriberDef subscriber)
{
    //...implementation removed

    return CreatedAtRoute("SubscriberLink", new { id = subscriber.ID });
}

例外是:

System.InvalidOperationException:没有路由与提供的值匹配.

System.InvalidOperationException: No route matches the supplied values.

该异常导致服务返回500状态代码.

The exception causes the service to return a 500 status code.

在任何一种情况下都是相同的路线,所以我不知道为什么第一个示例正确地工作而第二个示例却不正确.

It is the same route in either case, so I don't know why the first example works correctly and the second does not.

我的project.json包含以下内容:

"frameworks": {
  "dnx46": { },
  "dnxcore50": { }
},

为便于参考,命名路由由两部分组成.首先是控制器前缀:

For reference sake, the named route is composed from two pieces. First is the controller prefix:

[Route("api/[controller]")]
public class SubscribersController : Controller
{
    // ...
}

第二个是GET操作,其中实际的"SubscriberLink"路由被命名为:

Second is the GET action, where the actual "SubscriberLink" route is named:

[HttpGet("{id}", Name = "SubscriberLink")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(Subscriber))]
public async Task<IActionResult> GetSubscriber(Guid id)
{
    //...implementation removed...
    return Ok(subscriber);
}

有想法吗?

推荐答案

简短回答

您使用了错误的CreatedAtRoute重载.请改用带有三个参数的重载.

Short Answer

You are using the wrong overload of CreatedAtRoute. Use the overload that takes three arguments instead.

例如,以下内容适用于我的计算机.

For instance, the following works on my machine.

[Route("api/[controller]")]
public class SubscribersController : Controller
{
    public IActionResult Index()
    {
        var subscriber = new
        {
            Id = Guid.NewGuid(),
            FirstName = "Shaun",
            LastName = "Luttin"
        };

        // overload with three arguments
        return CreatedAtRoute(
            routeName: "SubscriberLink",
            routeValues: new { id = subscriber.Id },
            value: subscriber);
    }

    [HttpGet("{id}", Name = "SubscriberLink")]
    public IActionResult GetSubscriber(Guid id)
    {
        var subscriber = new
        {
            Id = id,
            FirstName = "Shaun",
            LastName = "Luttin"
        };

        return new JsonResult(subscriber);
    }
}

详细信息

结果是201响应.响应的正文包含我们创建的实体的详细信息(value参数),响应的Location标头包含该实体的URI.

Details

The result is a 201 response. The response's body contains details of the entity we created (the value argument) and the response's Location header contains a URI to the entity.

CreatedAtRoute有三个重载.

CreatedAtRoute(object routeValues, object value)
CreatedAtRoute(string routeName, object value)
CreatedAtRoute(string routeName, object routeValues, object value)

如果要传递路由名称和路由值,则使用带有三个参数的重载.如果我们不想返回创建的实体的详细信息,则可以为第三个参数

If we want to pass a route name and route values, we use the overload that takes three arguments. If we do not want to return details of the entity we created, we can pass null for the third argument,

这篇关于ASP.NET Core"CreatedAtRoute"失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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