当MVC2视图不存在时返回HTTP 404

Dmy*_*iak 1 .net asp.net-mvc view http-status-code-404 asp.net-mvc-2

我只需要一个类似CMS的小型控制器.最简单的方法是这样的:

public class HomeController : Controller {
    public ActionResult View(string name) {
        if (!ViewExists(name))
            return new HttpNotFoundResult();
        return View(name);
    }

    private bool ViewExists(string name) {
        // How to check if the view exists without checking the file itself?
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是如果没有可用的视图,如何返回HTTP 404?

可能我可以在适当的位置检查文件并缓存结果,但这感觉非常脏.

谢谢,
德米特里.

Dar*_*rov 6

private bool ViewExists(string name) {
    return ViewEngines.Engines.FindView(
        ControllerContext, name, "").View != null;
}
Run Code Online (Sandbox Code Playgroud)