'System.Web.WebPages.Html.HtmlHelper'不包含'ActionLink的定义

时间:2014-02-18 15:29:34

标签: c# asp.net asp.net-mvc razor

首先 - 我已经搜索了SO 并发现了许多问题同样的问题,尽管只是在我的web.config中添加<add namespace="System.Web.Mvc.Html" />并没有解决问题......

好的,所以我已经开始了一个基于Angular SPA模板的新项目 - 链接在这里:

http://visualstudiogallery.msdn.microsoft.com/5af151b2-9ed2-4809-bfe8-27566bfe7d83

我已将MVC升级到版本5&amp; webApi到版本2 - 与解决方案中的其他项目一致。

现在,在SPA网站的_layout页面中,我尝试使用@Html.ActionLink帮助程序,但不断收到错误'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'ActionLink

我确信这完全取决于某些web.config配置 - 我安装的软件包与模板配置中的设置之间存在某种冲突。

特别是这部分:

<system.web.webPages.razor>
    <host factoryType="System.Web.WebPages.Razor.WebRazorHostFactory, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.WebPages.WebPage">
      <namespaces>            
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

如果我将其更改为使用版本5,那么我会收到无法找到程序集(v5)等错误。

我对如何解决这一冲突的想法不足。

以下是我的SPA web.config

中的配置部分
<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>

如果我将版本更改为5,我会收到以下错误:

Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.WebPages.Razor, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.


=== Pre-bind state information ===
LOG: DisplayName = System.Web.WebPages.Razor, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///D:/Development/IS/IS/IS.TransactionDesk.SPA/
LOG: Initial PrivatePath = D:\Development\IS\IS\IS.TransactionDesk.SPA\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\Development\IS\IS\IS.TransactionDesk.SPA\web.config
LOG: Using host configuration file: C:\Users\Darren\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Web.WebPages.Razor, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Users/Darren/AppData/Local/Temp/Temporary ASP.NET Files/root/3407d437/8e7ba9a/System.Web.WebPages.Razor.DLL.
LOG: Attempting download of new URL file:///C:/Users/Darren/AppData/Local/Temp/Temporary ASP.NET Files/root/3407d437/8e7ba9a/System.Web.WebPages.Razor/System.Web.WebPages.Razor.DLL.
LOG: Attempting download of new URL file:///D:/Development/IS/IS/IS.TransactionDesk.SPA/bin/System.Web.WebPages.Razor.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Stack Trace: 


[FileLoadException: Could not load file or assembly 'System.Web.WebPages.Razor, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
   System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) +0
   System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName) +70
   System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark) +39
   System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) +37
   System.Web.Compilation.BuildManager.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase) +65
   System.Web.WebPages.Razor.WebRazorHostFactory.DefaultTypeFactory(String typeName) +39
   System.Web.WebPages.Razor.WebRazorHostFactory.CreateFactory(String typeName) +67
   System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +72
   System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfigCore(RazorWebSectionGroup config, String virtualPath, String physicalPath) +313
   System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig(String virtualPath, String physicalPath) +228
   System.Web.WebPages.Razor.WebRazorHostFactory.CreateHostFromConfig(String virtualPath) +38
   System.Web.WebPages.Razor.RazorBuildProvider.GetHostFromConfig() +51
   System.Web.WebPages.Razor.RazorBuildProvider.CreateHost() +57
   System.Web.WebPages.Razor.RazorBuildProvider.get_Host() +56
   System.Web.WebPages.Razor.RazorBuildProvider.EnsureGeneratedCode() +77
   System.Web.WebPages.Razor.RazorBuildProvider.get_CodeCompilerType() +54
   System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider) +59
   System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders() +209
   System.Web.Compilation.BuildProvidersCompiler.PerformBuild() +30
   System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +9881909
   System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +299
   System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +103
   System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +165
   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) +43
   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(String virtualPath, Type requiredBaseType) +28
   System.Web.WebPages.BuildManagerWrapper.CreateInstanceOfType(String virtualPath) +343
   System.Web.WebPages.VirtualPathFactoryExtensions.CreateInstance(IVirtualPathFactory factory, String virtualPath) +236
   System.Web.WebPages.VirtualPathFactoryManager.CreateInstanceOfType(String virtualPath) +261
   System.Web.WebPages.VirtualPathFactoryExtensions.CreateInstance(IVirtualPathFactory factory, String virtualPath) +132
   System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath(String virtualPath, IVirtualPathFactory virtualPathFactory) +58
   System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath(String virtualPath) +59
   IS.TransactionDesk.SPA.Routing.DefaultRouteHandler.GetHttpHandler(RequestContext requestContext) in d:\Development\IS\IS\IS.TransactionDesk.SPA\Routing\DefaultRouteHandler.cs:42
   System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +9598252
   System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

4 个答案:

答案 0 :(得分:8)

我正在使用VS 2013 Pro Update 2.这对我有用:

  1. 关闭Visual Studio
  2. 删除bin和obj文件夹
  3. 重新打开解决方案并重建
  4. 视图上没有错误
  5. 顺便说一句,没有必要在配置文件上进行任何更改,并且简单的清理和重建解决方案不起作用。

答案 1 :(得分:1)

您是否尝试过清理解决方案,删除

中的临时ASP.NET文件

C:\ WINDOWS \ Microsoft.NET \ Framework \ v4.xxxx \ Temporary ASP.NET Files

并重建?

答案 2 :(得分:0)

我刚刚为Visual Studio 2013安装了更新2,我的问题就消失了。

答案 3 :(得分:0)

我正在使用VS 2015,并且已解决此问题。

如果不存在配置文件,则需要检查Views / Web.Config文件,您可以复制另一个项目。在需要更改

上的名称空间之后

----- **视图/Web.Config ** ---------

<namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="YourProjectName" />
</namespaces>

----- **视图/Web.Config ** ---------

如果有配置文件,只需检查名称空间名称。