Return HTTP 404 when MVC2 view does not exist

Posted by Dmitriy Nagirnyak on Stack Overflow See other posts from Stack Overflow or by Dmitriy Nagirnyak
Published on 2010-05-06T10:56:38Z Indexed on 2010/05/06 10:58 UTC
Read the original article Hit count: 903

Filed under:
|
|
|
|

Hi,

I just need to have the a small CMS-like controller. The easiest way would be something like this:

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?
    }
}

The question is how to return HTTP 404 if the there is no view available?

Probably I can check the files in appropriate locations and cache the result, but that feels really dirty.

Thanks,
Dmitriy.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2