Asp.net core ActionResult参数含义
类有一系列方法,如Asp.net core ActionResult参数含义,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,类有一系列方法,如Created或createDataAction,它们需要value参数 [NonAction] public virtual CreatedAtActionResult CreatedAtAction(string actionName, string controllerName, object routeValues, object value) { return new Cr
Created
或createDataAction
,它们需要value
参数
[NonAction]
public virtual CreatedAtActionResult CreatedAtAction(string actionName,
string controllerName,
object routeValues,
object value)
{
return new CreatedAtActionResult(actionName, controllerName, routeValues, value);
}
上面的文档(非常差)说:
要在实体中设置格式的值
我不明白这个参数代表什么?它实际上既没有在CreateDataActionResult类本身中使用,也没有在基类中使用。此外,没有方法重载允许不使用它。该值是要作为响应返回的新对象,与传递给
Ok(value)
的对象相同
如果只想发回新的资源url,则可以传递null
。如果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);
}
这将返回带有newUser
的json响应,并创建一个url指向“”或新创建用户id的头
如果您不想返回json响应,只需传递
null
代码的最后一行return createDataAction(nameof(GetUser)、nameof(UserController)、new{id=newUser.id、newUser)
看起来缺少一个右括号,但我不确定在哪里。可能在id=newUser.id
No之后?@RonC:是的,修复了。当您直接在SO中键入代码而没有打开IDE进行验证时发生:P