Question

_AppStart.cshtml is not mentioned often, and when it is, it seems to be mentioned as part of WebMatrix / ASP.Net Web Pages as opposed to MVC proper.

Furthermore, this high-scoring StackOverflow answer claims that _AppStart.cshtml "is not used in MVC." This seems to be either misleading or just not true, because if you create a new empty MVC4 application in Visual Studio and put this in _AppStart.cshtml:

@{        
    throw new Exception("Uh oh!");
}

Your website doesn't work.

So what's the "official" recommendation on using _AppStart.cshtml? Is it an oversight that it works? Or is it deprecated in MVC5?

(I'm hoping that it's not deprecated or frowned-upon because it seems like a convenient way to allow site-wide configuration changes without re-deploying /bin and without dealing with global.asax or web.config.)

Was it helpful?

Solution

It's an official part of the Web Pages framework which is what the Razor View Engine is based on. It's just as much a part of Web Pages 3 (which is what MVC 5 uses) as previous versions of Web Pages.

There is no reason why you cannot use _AppStart.cshtml in MVC. You can also use other Web Pages features such as @helper and @function. All of these are likely to be frowned upon by some. So long as your boss or customer isn't among those 'some' you can use them freely.

If you make changes to AppStart, you get the same effect as if you had changed the web.config. The application will unload and restart.

AppStart was introduced as a simple way to access Application_Start for beginners (which Web Pages is really aimed at) without them having to understand global.asax and events.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top