我在 asp.net MVC 项目上工作,我将一些视图文件传输到其他项目并为它们启用"始终复制"(复制到主项目的 bin 文件夹中(。
我创建了一个自定义视图引擎:
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"
};
}
}
然后添加到global.ascx中的mvc ViewEngine
ViewEngines.Engines.Add(new CMSViewEngine());
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
但我的问题是:为了显示视图,我遇到了异常:
'~/bin/Views/Forms/Index.cshtml' 中的视图必须派生自 视图页、视图页、视图用户控件或 视图用户控件。
我尝试将web.config
添加到查看文件夹,并尝试添加@inherits System.Web.Mvc.WebViewPage
以查看文件,但不起作用。
我的 bin 视图文件的 web.config 文件是:
<?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>
如何解决此问题以加载视图文件?
解决:
我必须从类扩展CMSViewEngine
类RazorViewEngine
而不是WebFormViewEngine
(才能正常工作。
您正在使用WebFormViewEngine
,如果是这种情况,则不应在WebFormViewEngine
中注册剃须刀视图文件,您的CMSViewEngine
类应仅包含基于WebForm的视图文件扩展名,例如:
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"
};
}
}
以防万一您使用 .NET Core 执行此操作,您只需将视图的路径放在 bin 文件夹中并传递模型即可。
return View(@"binDebugnetcoreapp1.0ViewsTest.cshtml", new ReportLibrary.Person());