i work on asp.net mvc project and i transfer some view files to other project and enable "copy always" for them (to copy in bin folder of main project).
i create a custom view engine :
public class CMSViewEngine : WebFormViewEngine
{
public CMSViewEngine()
{
MasterLocationFormats = new string[]
{
"~/bin/Views/{1}/{0}.master",
"~/bin/Views/Shared/{0}.master"
};
ViewLocationFormats = new string[]
{
"~/bin/Views/{1}/{0}.cshtml",
"~/bin/Views/{1}/{0}.vbhtml",
"~/bin/Views/Shared/{0}.cshtml",
"~/bin/Views/Shared/{0}.vbhtml",
"~/bin/Views/{1}/{0}.aspx",
"~/bin/Views/{1}/{0}.ascx",
"~/bin/Views/Shared/{0}.aspx",
"~/bin/Views/Shared/{0}.ascx"
};
}
}
then added to mvc ViewEngine in global.ascx
ViewEngines.Engines.Add(new CMSViewEngine());
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
but my problem is: for showing view i get under exception:
The view at '~/bin/Views/Forms/Index.cshtml' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.
i try add web.config
to view folder and also try add @inherits System.Web.Mvc.WebViewPage
to view files but not worked.
my web.config file for bin view files is :
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.2.3.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" />
<add namespace="TestProj.ContactFormApp" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
<system.web>
<compilation>
<assemblies>
<add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
</system.web>
</configuration>
how can resolve this problem to load view file?
Resolved:
I must extend CMSViewEngine
class from RazorViewEngine
class (instead of WebFormViewEngine
) to work correctly.
It looks like you are using WebFormViewEngine
, if that is the case you should not registere razor view files in WebFormViewEngine
, your CMSViewEngine
class should only contain WebForm based view files extensions like:
public class CMSViewEngine : WebFormViewEngine
{
public CMSViewEngine()
{
MasterLocationFormats = new string[]
{
"~/bin/Views/{1}/{0}.master",
"~/bin/Views/Shared/{0}.master"
};
ViewLocationFormats = new string[]
{
"~/bin/Views/{1}/{0}.aspx",
"~/bin/Views/{1}/{0}.ascx",
"~/bin/Views/Shared/{0}.aspx",
"~/bin/Views/Shared/{0}.ascx"
};
}
}