asp.net-mvc WebActivator 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5812596/
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
What does WebActivator do?
提问by Shawn Mclean
This code was generated for me after added entity framework code-first for SQL Server CE using NuGet. They did no changes to any other file. The file SQLCEEntityFramework.cs
was created and placed in App_Start
folder.
此代码是在使用 NuGet 为 SQL Server CE 添加实体框架代码优先后为我生成的。他们没有更改任何其他文件。该文件SQLCEEntityFramework.cs
已创建并放置在App_Start
文件夹中。
Does this mean it automatically gets executed or something? The same thing happened when I added Ninject for MVC 3. No code was added to the global.ascx
file so I have no idea if its plug and play or I have to configure something.
这是否意味着它会自动执行?当我为 MVC 3 添加 Ninject 时发生了同样的事情。没有向global.ascx
文件添加代码,所以我不知道它是即插即用还是我必须配置一些东西。
[assembly: WebActivator.PreApplicationStartMethod(typeof(StackTorrents.WebUI.App_Start.SQLCEEntityFramework), "Start")]
采纳答案by John Farrell
According to:
根据:
http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx
http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx
This new attribute allows you to have code run way early in the ASP.NET pipeline as an application starts up. I mean way early, even before Application_Start. This happens to also be before code in your App_code folder (assuming you have any code in there) has been compiled. To use this attribute, create a class library and add this attribute as an assembly level attribute. A common place to add this would be in the AssemblyInfo.cs class within the Properties folder.
这个新属性允许您在应用程序启动时在 ASP.NET 管道中尽早运行代码。我的意思是很早,甚至在 Application_Start 之前。这恰好也是在您的 App_code 文件夹中的代码(假设您在那里有任何代码)被编译之前。要使用此属性,请创建一个类库并将此属性添加为程序集级属性。添加它的常见位置是在 Properties 文件夹中的 AssemblyInfo.cs 类中。
回答by George Mauer
To clarify, it gives you a way of hooking into several application start and application shutdown events WITHOUT having to change any existing code files (previously you had to edit Globals.asax.cs).
澄清一下,它为您提供了一种挂钩多个应用程序启动和应用程序关闭事件的方法,而无需更改任何现有代码文件(以前您必须编辑 Globals.asax.cs)。
This is mostly a big deal when making packages as these events are really useful for bootstrapping Http modules and it is really difficult to write code into existing files.
这在制作包时很重要,因为这些事件对于引导 Http 模块非常有用,并且很难将代码写入现有文件。