Dzm*_*voi 1 asp.net-core-mvc asp.net-core
ControllerBase类具有一堆类似Created或CreatedAtAction需要value参数的方法。
[NonAction]
public virtual CreatedAtActionResult CreatedAtAction(string actionName,
string controllerName,
object routeValues,
object value)
{
return new CreatedAtActionResult(actionName, controllerName, routeValues, value);
}
Run Code Online (Sandbox Code Playgroud)
它的文档(确实很差)说:
要在实体主体中格式化的值。
我不明白该参数代表什么?实际上,既不在CreatedAtActionResult类本身内,也没有在基类内使用它。此外,没有方法重载允许不使用它。
该值是要作为响应返回的新对象,将传递给Ok(value)。
null如果您只想回发新的资源URL,则可以通过。或者,如果您的Java脚本想要直接使用它,则发布它的值。
[HttpGet]
public IActionResult GetUser(int id)
{
var user = context.Users.SingleOrDefault(id);
if(user==null)
return NotFound();
return Ok(user);
}
[HttpPost]
public IActionResult CreateUser(UserViewModel user)
{
var newUser = new User { /* assign values */ };
context.Users.Add(newUser);
context.SaveChanges();
return CreatedAtAction(nameof(GetUser), nameof(UserController), new { id = newUser.Id }, newUser);
}
Run Code Online (Sandbox Code Playgroud)
这将返回json响应,newUser并创建一个带有url的标头,该标头指向“ http://example.com/user/5 ”或新创建的用户的ID。
如果您不希望返回json响应,则只需传递即可null。
| 归档时间: |
|
| 查看次数: |
1158 次 |
| 最近记录: |