2010-06-21 56 views
2

序言为什么在调用HtmlHelper.AntiForgeryToken时会出现TypeInitializationException?

我已经搜查,看看我是否可以在任何地方上在这里找到这个问题,我不能。 I previously asked on meta如果报告SO上的错误是有效的。

我的共识是,我应该从源头报告错误,如果我感受到社区的头脑,也许会问及回答SO的问题,以便其他人可能会遇到它。所以我在这里做我的社区位。

问题

我有一个要求Html.AntiForgeryToken它抛出一个FormatterGeneratorTypeInitializationException MVC视图。堆栈跟踪如下(道歉滚动条):

The virtual path '[path]' maps to another application, which is not allowed 

[ArgumentException: The virtual path '/Asp%20Net%20Bug%202008/Account/Register' maps to another application, which is not allowed.] 
    System.Web.CachedPathData.GetVirtualPathData(VirtualPath virtualPath, Boolean permitPathsOutsideApp) +11193138 
    System.Web.HttpContext.GetFilePathData() +61 
    System.Web.Configuration.HttpCapabilitiesBase.GetBrowserCapabilities(HttpRequest request) +124 
    System.Web.HttpRequest.get_Browser() +168 
    System.Web.UI.Page.SetIntrinsics(HttpContext context, Boolean allowAsync) +207 
    System.Web.UI.Page.ProcessRequest(HttpContext context) +232 
    System.Web.Mvc.TokenPersister.CreateFormatterGenerator() +459 
    System.Web.Mvc.FormatterGenerator..cctor() +10 

[TypeInitializationException: The type initializer for 'FormatterGenerator' threw an exception.] 
    System.Web.Mvc.AntiForgeryDataSerializer.get_Formatter() +38 
    System.Web.Mvc.AntiForgeryDataSerializer.Serialize(AntiForgeryData token) +181 
    System.Web.Mvc.HtmlHelper.GetAntiForgeryTokenAndSetCookie(String salt, String domain, String path) +405 
    System.Web.Mvc.HtmlHelper.AntiForgeryToken(String salt, String domain, String path) +13 
    System.Web.Mvc.HtmlHelper.AntiForgeryToken() +17 
    ASP.views_account_register_aspx.__RenderregisterContent(HtmlTextWriter __w, Control parameterContainer) in c:\Users\andras.zoltan\documents\visual studio 2008\projects\aspnetbug2008\aspnetbug2008\Views\Account\Register.aspx:17 
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +115 
    ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Users\andras.zoltan\documents\visual studio 2008\projects\aspnetbug2008\aspnetbug2008\Views\Shared\Site.Master:26 
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +115 
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240 
    System.Web.UI.Page.Render(HtmlTextWriter writer) +38 
    System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +89 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4240

更奇怪的是,当他们加载了该项目,并运行一个同事没有得到这个问题。

我的环境是VS2008和IIS7.5;这是一个MVC2项目。

回答

4

这是Asp.Net Mvc中的一个错误(绝对是2,大概也是1),我已经在博客上写了大约here(如果在我的博客打开时发生错误 - 只是刷新!),并且我已经向MVC团队here

如果Web应用程序驻留在包含空格的虚拟目录中,那么MVC用于为防伪造令牌生成数据序列化程序的内部机制将无法工作。它使用伪造的Asp.Net页面请求,但关于它伪造请求的方式会导致URL验证失败,并错误地将该URL标识为在当前应用程序之外。

当前的解决方法是从应用程序的目录名称中删除空格 - 连字符是一个不错的选择。我相信任何在路径中需要URL编码的字符都可能是个问题。

为什么你可能会得到这个错误,而一个同事不会在你的web项目中允许每个开发者拥有自己的web绑定。 Web项目的默认设置是使用Cassini--当然,这不会产生这个问题。

相关问题