asp.net-mvc ASP.NET:在应用程序的预启动初始化阶段无法调用此方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4626647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET: This method cannot be called during the application's pre-start initialization stage
提问by sqwerty
I'm trying to get an ASP.NET MVC 3 site running on IIS 6.0.
我正在尝试让一个在 IIS 6.0 上运行的 ASP.NET MVC 3 站点。
Currently when I request a page from the server it gives the following error:
目前,当我从服务器请求页面时,它会出现以下错误:
Parser Error Message: This method cannot be called during the application's pre-start initialization stage.
解析器错误消息:在应用程序的预启动初始化阶段无法调用此方法。
on this line:
在这一行:
<add name="MyMembershipProvider" type="NS.MyMembershipProvider" connectionStringName="MyDatabase" applicationName="/MySite"/>
I'm completely stumped and don't have much of a clue about the ASP.NET application lifecycle, let alone the differences between 6.0 and 7.0. Reading through the MSDN pages on it hasn't seemed to help much.
我完全被难住了,对 ASP.NET 应用程序生命周期一无所知,更不用说 6.0 和 7.0 之间的区别了。通读 MSDN 上的页面似乎没有多大帮助。
Does anyone have any insight or any good links for investigation? :)
有没有人有任何见解或任何好的调查链接?:)
回答by Gregtheitroade
Add this in your web.config (in the appSettings section):
在您的 web.config 中添加它(在 appSettings 部分):
<add key="enableSimpleMembership" value="false"/>
<add key="autoFormsAuthentication" value="false"/>
EDIT:
编辑:
For the ones who ask why, it is a known issue described in the mvc 3 release notesMore details here
回答by Justin Helgerson
After upgrading some of my applications from ASP.NET MVC3 to MVC4 I was getting this error. It was a result of the WebMatrix assemblies (WebMatrix.WebData.dll and WebMatrix.Data.dll). I removed those references and assemblies from the /bin directory and that took care of the issue.
将我的一些应用程序从 ASP.NET MVC3 升级到 MVC4 后,我收到此错误。这是 WebMatrix 程序集(WebMatrix.WebData.dll 和 WebMatrix.Data.dll)的结果。我从 /bin 目录中删除了这些引用和程序集,并解决了这个问题。
回答by Andy McCluggage
@Ek0nomik is right. We migrated from the MembershipProvider
to the new ExtendedMembershipProvider
allowing us to take advantage of some of the new functionality offered in the WebMatrixnamespace. By default Simple Membership is enabled for some reason so we had to disable it explicitly since we didn't want to go as far as using the SimpleMembershipProvider
.
@Ek0nomik 是对的。我们从 迁移MembershipProvider
到新的ExtendedMembershipProvider
允许我们利用WebMatrix命名空间中提供的一些新功能。默认情况下,由于某种原因启用了 Simple Membership,因此我们必须明确禁用它,因为我们不想使用SimpleMembershipProvider
.
All we had to do was add this to the web.config:
我们所要做的就是将它添加到 web.config 中:
<add key="enableSimpleMembership" value="false"/>
Having Simple Membership enabled caused the Provider initialisation code to execute before the Application_Start handler. Our app structure requires App_Start to be the first thing to execute. Personally I would always expect this but Simple Membership changes this behaviour. Beware.
启用 Simple Membership 会导致 Provider 初始化代码在 Application_Start 处理程序之前执行。我们的应用程序结构要求 App_Start 是第一个执行的东西。就我个人而言,我总是期望这一点,但 Simple Membership 改变了这种行为。谨防。
回答by David Hammond
Well, I just got this error, and it resulted from having accidentally copied a .cshtml into the root of my project. It wasn't even included in the project. Deleted that and the error went away. This was with MVC3 on IIS7. I imagine some of the people getting this problem are in the same boat.
好吧,我刚刚收到这个错误,这是由于不小心将 .cshtml 复制到了我的项目的根目录中。它甚至没有包含在项目中。删除它,错误就消失了。这是 IIS7 上的 MVC3。我想有些人遇到这个问题是在同一条船上。
回答by Chris Moschini
This is caused by any of a number of Reflection calls being made too early in an Application. It just so happens the Web.Config suggestions in other answers prevented one such Reflection call from being made. In my case however:
这是由在应用程序中过早进行的许多反射调用中的任何一个引起的。碰巧的是,其他答案中的 Web.Config 建议阻止了一个这样的 Reflection 调用。然而,就我而言:
I'm using Entity Framework, and ran update-database
. I got:
我正在使用实体框架,并运行update-database
. 我有:
This method cannot be called during the application's pre-start initialization phase.
在应用程序的预启动初始化阶段不能调用此方法。
As it turns out we had code that used a library which was recently modified to get all code in all namespaces/projects. Specifically, it called:
事实证明,我们的代码使用了最近修改过的库以获取所有命名空间/项目中的所有代码。具体来说,它称为:
System.Web.Compilation.BuildManager.GetReferencedAssemblies()
Kaboom. That caused this obscure error. EF Migrations run in a weirdo zone where the application is half running and half not, meaning the above method can never be called by any code Migrations would call on.
卡布姆。这导致了这个模糊的错误。EF 迁移运行在一个奇怪的区域,其中应用程序一半运行一半不运行,这意味着上述方法永远不会被迁移将调用的任何代码调用。