Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a 'default using' to all cshtml pages?

Tags:

I'm creating my first MVC.Net application and I find myself including @using Gideon.Core.Mvc; on almost every page. Is it possible to add it by default to all pages?

In Asp.Net I'm able to add default stuff for controls to the web.config, hopefully it can be done for MVC.Net as well.

like image 388
Kees C. Bakker Avatar asked May 16 '11 21:05

Kees C. Bakker


People also ask

How do you specify a common layout for all views?

The default _ViewStart. cshtml is included in the Views folder. It can also be created in all other Views sub-folders. It is used to specify common settings for all the views under a folder and sub-folders where it is created.

How do I change the default page in ASP NET MVC?

Just change the Controller/Action names to your desired default. That should be the last route in the Routing Table. In MVC 5. if you have a form login, when you click login on the home page, it will then still redirect to Home controller , not your custom controller specified in the route.

Is Cshtml a Razor?

cshtml files are razorpages or MVC views, they does not contain any C#-written client-side code. If you wan to do so, you must use JavaScript. However, a . razor file, also know as a Razor component, can have C# written in it and run on client's browser.


1 Answers

You can add them in the <system.web.webPages.razor> section in Views/Web.config.

Here is the default:

<system.web.webPages.razor>     <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />     <pages pageBaseType="System.Web.Mvc.WebViewPage">         <namespaces>             <add namespace="System.Web.Mvc" />             <add namespace="System.Web.Mvc.Ajax" />             <add namespace="System.Web.Mvc.Html" />             <add namespace="System.Web.Routing" />         </namespaces>     </pages> </system.web.webPages.razor> 
like image 124
SLaks Avatar answered Oct 19 '22 12:10

SLaks