Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 MVC2中未找到HttpNot的替代方案_C# 4.0_Asp.net Mvc 2 - Fatal编程技术网

C# 4.0 MVC2中未找到HttpNot的替代方案

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

我正在尝试从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 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谢谢。。我对网络开发相当陌生。我想我需要看看这些教程中自动生成的代码背后发生了什么