在Web 2.2中,我们可以通过从控制器返回位置头URL,如下所示:
return Created(new Uri(Url.Link("GetClient", new { id = clientId })), clientReponseModel);
Url.Link(..)
将根据控制器名GetClient
相应地解析资源URL
在ASP.NET 5 MVC 6的Web中,Url
不存在于框架中,但是CreatedResult
构造函数确实具有位置参数:
return new CreatedResult("http://www.myapi.com/api/clients/" + clientId, journeyModel);
如何解决这个URL,而无需手动提供,就像我们在WebAPI2.2中所做的那样?
发布于 2015-08-13 15:15:16
我没有意识到这一点,但是CreatedAtAction()
方法满足了这一点:
return CreatedAtAction("GetClient", new { id = clientId }, clientReponseModel);
确保您的控制器来自MVC的Controller。
发布于 2017-04-28 14:41:51
在新的ASP.NET MVC核心中有一个属性Url
,它返回IUrlHelper
的一个实例。您可以使用以下命令来生成本地URL:
[HttpPost]
public async Task<IActionResult> Post([FromBody] Person person)
{
_DbContext.People.Add(person);
await _DbContext.SaveChangesAsync();
return Created(Url.RouteUrl(person.Id), person.Id);
}
发布于 2015-08-12 10:50:03
有一个UrlHelper
类实现IUrlHelper
接口。它提供了所请求的功能。
https://stackoverflow.com/questions/31957060
复制相似问题