简体   繁体   中英

ASP.NET Core “CreatedAtRoute” Failure

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);
}

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 });
}

The exception is:

System.InvalidOperationException: No route matches the supplied values.

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.

My project.json contains this:

"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
{
    // ...
}

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);
}

Thoughts?

Short Answer

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

Working Example

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);
    }
}

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.

响应包括我们创建的obj及其位置。

There are three overloads for 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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM