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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…