asp.net-mvc Visual Studio 2013 项目中的新 Startup.cs 文件是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20034105/
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 is the new Startup.cs file for in Visual Studio 2013 projects?
提问by Mark Redman
I have just installed Visual Studio 2013, created an MVC Web Application project and noticed a new file in the project template called Startup.cs.
我刚刚安装了 Visual Studio 2013,创建了一个 MVC Web 应用程序项目,并注意到项目模板中有一个名为 Startup.cs 的新文件。
What is this, how is this different from Global.asax.cs and are there any good best practices on what to use this for?
这是什么,这与 Global.asax.cs 有什么不同,是否有关于如何使用它的最佳最佳实践?
采纳答案by Lin
Every OWIN application has a startup class where you specify components for the application pipeline.
每个 OWIN 应用程序都有一个启动类,您可以在其中指定应用程序管道的组件。
If you start a new Visual Studio project, you'll see pieces of OWIN in it. OWIN is a specification that defines an API for framework and servers to cooperation. The point of OWIN is to decouple server and application. For example, ASP.NET Identity uses OWIN security, SignalR self hosting uses OWIN hosting, and etc., the examples all use OWIN, therefore they all need to have a startup class, that is defined in "Startup.cs" file.
如果您启动一个新的 Visual Studio 项目,您将在其中看到 OWIN 的片段。OWIN 是一个规范,它定义了框架和服务器协作的 API。OWIN 的重点是解耦服务器和应用程序。例如,ASP.NET Identity 使用 OWIN 安全,SignalR 自托管使用 OWIN 托管等,示例都使用 OWIN,因此它们都需要有一个启动类,在“Startup.cs”文件中定义。
The Global.asax, the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET or by HttpModules.
ASP.NET 应用程序文件 Global.asax 是一个可选文件,其中包含用于响应由 ASP.NET 或 HttpModules 引发的应用程序级事件的代码。
For more details:
更多细节:
OWIN
欧文
http://www.asp.net/aspnet/overview/owin-and-katana
http://www.asp.net/aspnet/overview/owin-and-katana
Global.asax
全球.asax
http://msdn.microsoft.com/en-us/library/1xaas8a2(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/1xaas8a2(v=vs.71).aspx
You can find more ideas about why OWIN in the following article:
您可以在以下文章中找到有关 OWIN 的更多想法:
http://www.asp.net/aspnet/overview/owin-and-katana/an-overview-of-project-katana
http://www.asp.net/aspnet/overview/owin-and-katana/an-overview-of-project-katana
回答by user247702
The file seems to be related to SignalR. Quoting the VS 2013 release notes:
该文件似乎与 SignalR 有关。引用VS 2013 发行说明:
Built on OWIN
SignalR 2.0 is built completely on OWIN (the Open Web Interface for .NET). This change makes the setup process for SignalR much more consistent between web-hosted and self-hosted SignalR applications, but has also required a number of API changes.
MapHubs and MapConnection are now MapSignalR
For compatibility with OWIN standards, these methods have been renamed to MapSignalR. MapSignalR called without parameters will map all hubs (as MapHubs does in version 1.x); to map individual PersistentConnection objects, specify the connection type as the type parameter, and the URL extension for the connection as the first argument.
The MapSignalR method is called in an Owin startup class. Visual Studio 2013 contains a new template for an Owin startup class; to use this template, do the following:
- Right-click on the project
- Select Add, New Item...
- Select Owin Startup class. Name the new class Startup.cs.
In a Web application, the Owin startup class containing the MapSignalR method is then added to Owin's startup process using an entry in the application settings node of the Web.Config file, as shown below.
In a Self-hosted application, the Startup class is passed as the type parameter of the WebApp.Start method.
基于 OWIN
SignalR 2.0 完全基于 OWIN(.NET 的开放 Web 界面)构建。此更改使 SignalR 的设置过程在 Web 托管和自托管 SignalR 应用程序之间更加一致,但也需要一些 API 更改。
MapHubs 和 MapConnection 现在是 MapSignalR
为了与 OWIN 标准兼容,这些方法已重命名为 MapSignalR。不带参数调用的 MapSignalR 将映射所有集线器(就像 MapHubs 在 1.x 版中所做的那样);要映射单个 PersistentConnection 对象,请将连接类型指定为类型参数,并将连接的 URL 扩展名指定为第一个参数。
MapSignalR 方法在 Owin 启动类中调用。Visual Studio 2013 包含一个用于 Owin 启动类的新模板;要使用此模板,请执行以下操作:
- 右键单击项目
- 选择添加、新建项目...
- 选择 Owin 启动类。将新类命名为 Startup.cs。
在 Web 应用程序中,然后使用 Web.Config 文件的应用程序设置节点中的条目将包含 MapSignalR 方法的 Owin 启动类添加到 Owin 的启动过程中,如下所示。
在自承载应用程序中,Startup 类作为 WebApp.Start 方法的类型参数传递。
回答by cecilphillip
The Startup class is the convention that Katana/OWIN looks for to initialize the pipeline. When your app starts, the code inside of the Configuration function is run to set up the components that'll be used. In the MVC 5 templates, it's used to wire up the authentication middleware which is all built on top of OWIN.
Startup 类是 Katana/OWIN 寻找初始化管道的约定。当您的应用程序启动时,将运行 Configuration 函数中的代码来设置将要使用的组件。在 MVC 5 模板中,它用于连接所有构建在 OWIN 之上的身份验证中间件。
If you want to use dependency injection with OWIN, check out this project on GitHub: DotNetDoodle.Owin.Dependencies
如果你想在 OWIN 中使用依赖注入,请查看 GitHub 上的这个项目:DotNetDoodle.Owin.Dependencies

