【问题标题】:Calling Html.Image from Razor view results in TypeAccessException从 Razor 视图调用 Html.Image 会导致 TypeAccessException
【发布时间】:2015-02-19 13:39:21
【问题描述】:

我已经为此苦苦挣扎了好几个小时。我只是想在 Razor 视图中使用 Image 助手,我得到了 TypeAccessException

这是有问题的代码行:

@Html.Image(Content.images.Profile_gif)

这是一个例外:

尝试通过安全透明方法“Microsoft.Web.Mvc.ImageExtensions.Image(System.Web.Mvc.HtmlHelper, System.String)”访问安全关键类型“System.Web.Mvc.MvcHtmlString”失败。 H3>

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息: System.TypeAccessException:尝试通过安全透明方法“Microsoft.Web.Mvc.ImageExtensions.Image(System.Web.Mvc.HtmlHelper, System.String)”访问安全关键键入“System.Web.Mvc.MvcHtmlString”失败。

查看下面的完整堆栈跟踪。


这是在 MVC5 应用程序中,当我在 Visual Studio 2012 中以调试或发布模式进行调试时,目前正在发生问题。大部分代码来自 MVC3 应用程序,但我实际上是从一个新的 MVC5 项目开始并复制的在文件夹中。我不记得最初遇到过这个异常,但它似乎在一些错误修复和 NuGet 更新后悄悄出现。 (编辑:我刚刚恢复到旧版本以确认这一点,但异常仍然发生,所以看起来这个异常一直是一个问题,除非我的问题与 IIS Express 有关。)

以下是一些我认为对我有帮助但没有帮助的关键主题:


许多建议的解决方案似乎涉及确保安装正确和最新的 NuGet 包,所以这是我的 packages.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AutoMapper" version="3.3.0" targetFramework="net45" />
  <package id="BCrypt-Official" version="0.1.109" targetFramework="net45" />
  <package id="elmah" version="1.2.2" targetFramework="net45" />
  <package id="elmah.corelibrary" version="1.2.2" targetFramework="net45" />
  <package id="Elmah.MVC" version="2.1.1" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.1" targetFramework="net45" />
  <package id="EntityFramework.SqlServerCompact" version="6.1.1" targetFramework="net45" />
  <package id="FluentValidation" version="5.5.0.0" targetFramework="net45" />
  <package id="FluentValidation.MVC5" version="5.5.0.0" targetFramework="net45" />
  <package id="jQuery" version="2.1.1" targetFramework="net45" />
  <package id="jQuery.UI.Combined" version="1.11.2" targetFramework="net45" />
  <package id="jQuery.Validation" version="1.13.1" targetFramework="net45" />
  <package id="MailChimp.NET" version="1.1.65.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebHelpers" version="3.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.Data" version="3.2.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.WebData" version="3.2.2" targetFramework="net45" />
  <package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.2" targetFramework="net45" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.2" targetFramework="net45" />
  <package id="Microsoft.SqlServer.Compact" version="4.0.8876.1" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Modernizr" version="2.8.3" targetFramework="net45" />
  <package id="Mvc4Futures" version="4.0.20710.0" targetFramework="net45" />
  <package id="MvcContrib.Mvc5" version="5.0.0.0" targetFramework="net45" />
  <package id="MvcMailer" version="4.5" targetFramework="net45" />
  <package id="Ninject" version="3.2.2.0" targetFramework="net45" />
  <package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net45" />
  <package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net45" />
  <package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net45" />
  <package id="ServiceStack.Text" version="3.9.71" targetFramework="net45" />
  <package id="T4MVC" version="3.10.0" targetFramework="net45" />
  <package id="T4MVCExtensions" version="3.10.0" targetFramework="net45" />
  <package id="T4Scaffolding.Core" version="1.0.0" targetFramework="net45" />
  <package id="WebActivatorEx" version="2.0.6" targetFramework="net45" />
</packages>

我运行Update-Package -Reinstall 以确保每个 NuGet 包都已正确安装。我还尝试删除包文件夹并让 NuGet 包恢复再次下载所有内容。这些都没有任何区别。


以下是我的 Web.config 的一些相关部分:

<appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="FluentValidation" publicKeyToken="a82054b837897c66" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-3.4.5.0" newVersion="3.4.5.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
        </dependentAssembly>
        <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="0.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.2.2.0" newVersion="5.2.2.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

<system.web>
    <trust level="Full" />
    <customErrors mode="RemoteOnly" defaultRedirect="Error.htm" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
        <forms loginUrl="~/Account/LogOn" timeout="10" />
    </authentication>
    <pages>
        <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.Routing" />
            <add namespace="System.Web.WebPages" />
        </namespaces>
    </pages>
</system.web>

最后,这是异常的完整堆栈跟踪:

[TypeAccessException: Attempt by security transparent method 'Microsoft.Web.Mvc.ImageExtensions.Image(System.Web.Mvc.HtmlHelper, System.String)' to access security critical type 'System.Web.Mvc.MvcHtmlString' failed.]
   Microsoft.Web.Mvc.ImageExtensions.Image(HtmlHelper helper, String imageRelativeUrl) +0
   ASP._Page_Views_Member_ProfileLink_cshtml.Execute() in d:\Projects\Current\Tennis Club\MVC5\Solution\TennisClub\Views\Member\ProfileLink.cshtml:5
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +122
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +145
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.Mvc.<>c__DisplayClassa.<EndProcessRequest>b__9() +44
   System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +34
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +69
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Action action) +123
   System.Web.Mvc.ServerExecuteHttpHandlerAsyncWrapper.EndProcessRequest(IAsyncResult result) +133
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1464

[HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +3177611
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +76
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +28
   System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +19
   System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +979
   System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) +112
   System.Web.Mvc.T4Extensions.Action(HtmlHelper htmlHelper, ActionResult result) +125
   ASP._Page_Views_Member_Filtered_cshtml.<Execute>b__2() in d:\Projects\Current\Tennis Club\MVC5\Solution\TennisClub\Views\Member\Filtered.cshtml:17
   System.Web.WebPages.<>c__DisplayClass7.<RenderSection>b__5(TextWriter tw) +352
   System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +42
   System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content) +45
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +53
   ASP._Page_Views_Shared_StandardLayouts_Index_cshtml.Execute() in d:\Projects\Current\Tennis Club\MVC5\Solution\TennisClub\Views\Shared\StandardLayouts\Index.cshtml:13
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +122
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +145
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer) +41
   System.Web.WebPages.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer) +335
   System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +42
   System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content) +45
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +53
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +178
   System.Web.WebPages.WebPageBase.PopContext() +229
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +154
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9657896
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 html-helper


    【解决方案1】:

    当我之前查看 NuGet 包列表时,我注意到其中一项相当可疑,因为它的名称包含“Mvc4”而不是“Mvc5”:

    <package id="Mvc4Futures" version="4.0.20710.0" targetFramework="net45" />
    

    但是,我不认为这是问题的根源,因为这个包是 MvcContrib.Mvc5 的依赖项,而且我认为我没有(直接)将它用于任何事情。

    好吧,不知何故,我在第一次调查时错过了这一点,但事实证明 ASP.NET MVC5 中没有 Image HtmlHelper——它是由 Mvc4Futures 添加的。命名空间 Microsoft.Web.Mvc 骗我以为它是核心 ASP.NET MVC 的一部分,但在 MVC5 中,它看起来核心命名空间是 System.Web.Mvc

    这个问题有两种解决方案:

    1. 只是为了使用纯 HTML:

      <img src="@Content.images.Profile_gif" />
      
    2. 保持代码原样(使用Image 助手),但将Mvc4Futures 包替换为Microsoft.AspNet.Mvc.Futures 包(这需要使用Uninstall-Package -force Mvc4Futures,因为MvcContrib.Mvc5 依赖于它)。

    编辑

    我随后在 CodePlex 上的 MvcContrib 项目站点上添加了一个问题,要求从 Mvc4Futures 更改依赖项 --> Microsoft.AspNet.Mvc.Futures:

    https://mvccontrib.codeplex.com/workitem/7229

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多