C# 4.0 MVC2中未找到HttpNot的替代方案
我正在尝试从ASP.Net网站示例应用程序学习ASP.NETMVC。此应用程序是为MVC3编写的。我被困在下面的部分。有人能帮我解决这个问题吗C# 4.0 MVC2中未找到HttpNot的替代方案,c#-4.0,asp.net-mvc-2,C# 4.0,Asp.net Mvc 2,我正在尝试从ASP.Net网站示例应用程序学习ASP.NETMVC。此应用程序是为MVC3编写的。我被困在下面的部分。有人能帮我解决这个问题吗 public ActionResult Edit(int id = 0) { Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } return View(movie); } 错误: The
public ActionResult Edit(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
错误:
The Name 'HTTPNotFound' does not exist in the current context.
该方法及其相关继承器(如HttpNotFoundResult
)是MVC3中添加的新特性。这就是为什么MVC2不起作用的原因
因此,要么升级到MVC3(甚至本周MVC4也以RTM的形式发布)
或者您可以创建自己的ActionResult
以返回404状态代码:
public class HttpNotFoundResult : ActionResult
{
private const int NotFoundCode = 404;
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
context.HttpContext.Response.StatusCode = NotFoundCode;
}
}
在你的行动方法中:
public ActionResult Edit(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return new HttpNotFoundResult();
}
return View(movie);
}
如果Move为null,您想做什么?向用户返回一条消息,指出找不到任何记录可以使用Model.Errors或ViewBag谢谢。。我对网络开发相当陌生。我想我需要看看这些教程中自动生成的代码背后发生了什么