Question

I've deleted my _viewstart file, but I'm worried because my application keeps running okay.

I've noticed this problem when I tried to include a style sheet at my app.

Then my questions are:

1 - I must to create a _viewstart file, or I have to recover my _viewstart deleted file?

2 - How can I add a css to my views?

PS.: Please, if you need some code ask me in comments.

Was it helpful?

Solution

Answering your questions:

1) If you didn't created your own custom _Layout.cshtml file then you can recover it or just add new file _ViewStart.cshtml with following code in it:

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

If you got your own _layout then you obviously have to create it again (if you want to):

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

If you don't use a _ViewStart.cshtml file in your project then you have to specify in every View file the path of your _Layout.chtml file with Layout property:

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

But this will cause you a headache in the future. Imagine you have a 100 of View files (wich contains Layout = "~/Views/Shared/_MyAwesomeLayout.cshtml"; in it, and after a while you want to rename the name of your layout or change the location of your layout file...then you're going to find every View that refers to it...(that will be a mess :)). And here the _ViewStart.cshtml cames in..., where you can set your layout (in a single place). The ASP.NET MVC framework, when it renders a View will look for a file called _ViewStart.cshtml and the content of this file will be treated as though they were contained in the View file itself.

2) By default in MVC projects the CSS files are in Content file, so you can add your own CSS files by drag and drop in Content folder (or of course you can make your own folder structure in your project).

The easy way to add a CSS file in your View is to drag and drop your CSS file from Solution Explorer in the View itself and the Visual Studio (automatically) will show the path of your CSS file (or you can add by yourself).

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