Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a common _ViewStart in areas?

In my "root" Views folder, I have a _ViewStart with the following code:

@Code     Layout = "~/Views/Shared/_Layout.vbhtml" End COde 

In my Area/Public/Views folder, I have a copy of my _ViewStart from the root Views folder.

But when I run the code, I get this error:

Unable to cast object of type 'ASP._ViewStart_vbhtml' to type 'System.Web.WebPages.StartPage'. 

What I'm doing wrong?

Can I use one _ViewStart.vbhtml for my areas too?

How can I use _ViewStart.vbhtml in Areas?

like image 561
MojoDK Avatar asked Nov 05 '10 19:11

MojoDK


People also ask

What is the purpose of _ViewStart Cshtml?

The _ViewStart. cshtml page is a special view page containing the statement declaration to include the Layout page. Instead of declaring the Layout page in every view page, we can use the _ViewStart page. When a View Page Start is running, the “_ViewStart.

What is the use of ViewStart?

The primary use of a ViewStart file is to set the Layout view. Let us now go to the Index. cshtml file and cut the Layout line and then add it to the ViewStart file as shown in the following program.

Can we have multiple _ViewStart in MVC?

We can also create multiple _ViewStart. cshtml pages. The file execution is dependent upon the location of the file within the folder hierarchy and the view being rendered. The MVC Runtime will first execute the code of the _ViewStart.

What is ASP Area used for?

Areas are an ASP.NET feature used to organize related functionality into a group as a separate namespace (for routing) and folder structure (for views). Using areas creates a hierarchy for the purpose of routing by adding another route parameter, area , to controller and action or a Razor Page page .


1 Answers

You need to copy the ~\Views\Web.config file (or at least the following configuration elements) into your Area's View Web.Config:

<configSections>   <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">     <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />     <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />   </sectionGroup> </configSections>  <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 158
marcind Avatar answered Sep 19 '22 23:09

marcind