【问题标题】:ASP.NET MVC 5 The view at '~/Views/Shared/DisplayTemplates/SiteMapPathHelperModel.cshtml' must derive from WebViewPage, or WebViewPage<TModel>ASP.NET MVC 5 '~/Views/Shared/DisplayTemplates/SiteMapPathHelperModel.cshtml' 处的视图必须派生自 WebViewPage 或 WebViewPage<TModel>
【发布时间】:2014-11-02 16:25:02
【问题描述】:

从 mvc3 升级到 mvc5 后,我遇到了 MvcSiteMapProvider 的问题。 我使用以下简单的 web.config 测试了一个简单的测试视图,@Html.MvcSiteMap().SiteMapPath() 工作正常。

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

    <add key="MvcSiteMapProvider_UseExternalDIContainer" value="false" />
    <add key="MvcSiteMapProvider_IncludeAssembliesForScan" value="XXX.WebMVC" />
    <add key="MvcSiteMapProvider_ScanAssembliesForSiteMapNodes" value="true" />
    <add key="MvcSiteMapProvider_EnableSiteMapFile" value="false" />
    <add key="MvcSiteMapProvider_IncludeRootNodeFromSiteMapFile" value="true" />

  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="MvcSiteMapProvider.Web.Html" />
        <add namespace="MvcSiteMapProvider.Web.Html.Models" />
      </namespaces>
    </pages>
  </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

</configuration>

但我的 web.config 要复杂得多。我更新了所有相应的部分,但仍然遇到堆栈跟踪错误:

“~/Views/Shared/DisplayTemplates/SiteMapPathHelperModel.cshtml”处的视图必须派生自 WebViewPage 或 WebViewPage。

[InvalidOperationException: The view at '~/Views/Shared/DisplayTemplates/SiteMapPathHelperModel.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.]
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +401
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions) +544
   System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate) +1089
   System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData) +66
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper) +119
   System.Web.Mvc.Html.TemplateHelpers.TemplateFor(HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData) +104
   MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper helper, String templateName, ISiteMapNode startingNode, SourceMetadataDictionary sourceMetadata) +232
   MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper helper, String templateName, String startingNodeKey, SourceMetadataDictionary sourceMetadata) +70
   MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper helper) +45
   ASP._Page_Views_Home_Test_cshtml.Execute() in c:\Projects\Drive\drive\Src\Web\Views\Home\Test.cshtml:9
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +198
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +64
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +256
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9657896
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我把断点放在@Html.MvcSiteMap().SiteMapPath(),在Watch windows中@Html.MvcSiteMap()引发异常

@Html.MvcSiteMap()  'System.Web.Mvc.HtmlHelper<object>' does not contain a definition for 'MvcSiteMap' and the best extension method overload 'MvcSiteMapProvider.Web.Html.HtmlHelperExtensions.MvcSiteMap(System.Web.Mvc.HtmlHelper)' has some invalid arguments   

我的观点/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.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" />
        <add namespace="MvcSiteMapProvider.Web.Html" />
        <add namespace="MvcSiteMapProvider.Web.Html.Models" />
      </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>
</configuration>

您能否告诉我最有可能导致错误的区域是什么?

【问题讨论】:

    标签: asp.net-mvc-5 mvcsitemapprovider razorengine


    【解决方案1】:

    MVC 版本之间没有直接的升级路径,必须卸载 MVC 3 NuGet 包并安装 MVC 5 NuGet 包。每个 MVC 版本都有一个单独的 MvcSiteMapProvider DLL,因此程序集绑定重定向无法解决此问题。

    1. 备份您的解决方案,以免丢失任何东西。
    2. 在包管理器控制台中运行PM&gt; Uninstall-Package MvcSiteMapProvider.MVC3
    3. 确保 MVC 5.x 仍包含在您的 packages.config 文件中。如果没有,请重新安装它PM&gt; Install-Package Microsoft.AspNet.Mvc
    4. 在包管理器控制台中运行PM&gt; Install-Package MvcSiteMapProvider.MVC5。当被问及是否覆盖任何文件时回答“否”(这些将是您自定义的文件)。

    MvcSiteMapProvider.Web 包将检测您的项目是 MVC 5 并使用正确的设置更新您的 Web.Config 文件,以便它可以在 MVC 5 中工作,因此您必须确保在安装 MvcSiteMapProvider 之前将 MVC 5 安装到项目中。

    完成后,仔细检查您的 packages.config 文件以确保它安装了正确的版本。您的版本和 targetFramework 可能与此不同,但您绝对应该拥有 MVC 5 包。

    <package id="MvcSiteMapProvider.MVC5" version="4.6.17" targetFramework="net451" />
    <package id="MvcSiteMapProvider.MVC5.Core" version="4.6.17" targetFramework="net451" />
    <package id="MvcSiteMapProvider.Web" version="4.6.1" targetFramework="net451" />
    

    如果有任何问题,或者如果您希望手动配置而不是使用 NuGet,您始终可以将 MvcSiteMapProvider.MVC5 添加到新的 MVC 5 项目,然后协调新项目与当前项目之间的配置差异一个。

    【讨论】:

    • 在我的情况下 - 这是 web.config 中的这个人创建问题: (它有 5.0而不是正确的 5.1) 我使用的是 mvc5。
    猜你喜欢
    • 2011-12-28
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2013-06-05
    • 2016-02-05
    • 1970-01-01
    相关资源
    最近更新 更多