Dav*_*New 9 asp.net-core-mvc dnx asp.net-core
在Web Api 2.2中,我们可以通过从控制器返回来返回位置标头URL,如下所示:
return Created(new Uri(Url.Link("GetClient", new { id = clientId })), clientReponseModel);
Run Code Online (Sandbox Code Playgroud)
Url.Link(..)将根据控制器名称相应地解析资源URL GetClient:
在ASP.NET 5 MVC 6的Web Api中,Url框架中不存在但CreatedResult构造函数确实具有location参数:
return new CreatedResult("http://www.myapi.com/api/clients/" + clientId, journeyModel);
Run Code Online (Sandbox Code Playgroud)
如何在不必手动提供此URL的情况下解析此URL,就像我们在Web API 2.2中所做的那样?
Dav*_*New 14
I didn't realise it, but the CreatedAtAction() method cater for this:
return CreatedAtAction("GetClient", new { id = clientId }, clientReponseModel);
Run Code Online (Sandbox Code Playgroud)
Ensure that your controller derives from MVC's Controller.
在新的ASP.NET MVC Core中有一个属性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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9489 次 |
| 最近记录: |