Wednesday 2 April 2014

what is the purpose of _viewstart.cshtml in ASP.NET MVC?

The _ViewStart.cshtml file will execute at the start of each view's rendering. Any code contained within the code block in this file will execute before any code in the view. Typically, this file will set the layout template to be used by the views in the application:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering. So we can optionally make our Layout selection logic richer than just a basic property set,if required.

Because this code executes at the start of each View, we no longer need to explicitly set the Layout in any of our individual view files (except if we wanted to override the default value above).

An MVC application can have multiple _ViewStart.cshtml files. The order in which these files execute is dependent upon the location of the files in the folder hierarchy and the particular view being rendered. The MVC Runtime will first execute the code in the _ViewStart.cshtml file located in the root of the Views folder. It will then work its way up the folder hierarchy, executing the code in each _ViewStart.cshtml file it finds along the way. For example, if we had_ViewStart.cshtml files in the following locations:

1) Views\_ViewStart.cshtml
2) Views\Home\_ViewStart.cshtml
3) Views\Products\_ViewStart.cshtml

When a view in the Views\Home folder is rendered, the MVC Runtime will first execute the code in file 1 and then execute the code in file 2. Since file 3 is not in the hierarchy of the Views\Home folder, the code in file 3 will not get executed.

Visual Studio 2012 - class names lost default colour and appear in black

Recently some settings on my Visual Studio 2012 got changed somehow and I lost most of the default colour settings in the IDE text editor for C#. Also "User Types" was missing from "Display items" list of "Tools > Options > Environment > Fonts and Colors" dialog box. So I was unable change the colour manually. I managed to bring back the default settings by doing the following.

  1. Delete folders
    • %appdata%\Roaming\Microsoft\Microsoft Visual Studio
    • %appdata%\Roaming\Microsoft\VisualStudio
    • %appdata%\Local\Microsoft\VisualStudio
    • My Documents/Visual Studio 2012/Settings
  2. Open Visual Studio 2012 command prompt (as administrator) and run
    • devenv.exe /setup
    • devenv.exe /ResetSettings
    *devenv.exe location is usually something like C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE.
I think the first step is irrelevant. So if you experience this problem I would suggest you skip step#1 and just follow step#2.

References

no-intellisense-no-indentation

class-names-not-coloured-in-visual-studio

how-can-i-get-user-type-c-sharp-syntax-highlighting-working-again