让我们以一个全新的ASP为例。NET MVC 5应用程序从MVC与个人帐户模板,如果我删除Global.asax.cs类并将其配置代码移动到Startup.cs configuration()方法如下,有什么缺点?

public partial class Startup
{
     public void Configuration(IAppBuilder app)
     {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        ConfigureAuth(app);
    }
}

对我来说,好处是在升级ASP时。asp.net 4应用到ASP。NET 5和使用现在必须在Startup.cs类中配置的部分,我没有在两个不同的类中进行依赖注入和其他配置,这两个类似乎与启动和配置有关。


启动。Configuration被调用的时间比Application_Start稍微晚一些,但我认为在大多数情况下,这种差异不会有太大影响。

我相信我们把其他代码保留在全局的主要原因。asax是:

Consistency with previous versions of MVC. (That's where everybody currently expects to find this code.) Ability to add other event handlers. In Global.asax, you can handle other methods like Session_Start and Application_Error. Correctness in a variety of authentication scenarios. The Startup.Configuration method is only called if you have Microsoft.Owin.Host.SystemWeb.dll in your bin directory. If you remove this DLL, it will silently stop calling Startup.Configuration, which could be hard to understand.

我认为第三个原因是最重要的一个原因,我们没有默认采用这种方法,因为一些场景不包括有这个DLL,而且能够更改身份验证方法而不使放置不相关代码(如路由注册)的位置无效是很好的。

但是如果这些原因都不适用于您的场景,我认为您可以使用这种方法。


对于那些正在寻找完整步骤的人:如果你正在寻找一个基于OWIN的,IIS托管的web API,这些步骤应该会让你到达那里:

File -> New -> Project In the dialogue, Installed -> templates -> Other Project types -> Visual Studio Solutions -> Blank Solution targeting .NET 4.6 On the solution, right click, add Project -> Web -> ASP.NET Web Application (targeting .NET 4.6) 3.1 Now In the ASP.NET 4.5 templates, choose Empty as the template 3.2 This creates a blank solution with two nuget packages: Microsoft.CodeDom.Providers.DotNetCompilerPlatform v 1.0.0 Microsoft.Net.Compilers v 1.0.0 Install the following packages: Install-Package Microsoft.AspNet.WebApi.WebHost -Version 5.2.3 Install-Package Microsoft.AspNet.WebApi -Version 5.2.3 Install-Package WebApiContrib.Formatting.Razor 2.3.0.0

OWIN:

Install-Package Microsoft.Owin.Host.SystemWeb 
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost    

然后用Configuration方法添加Startup.cs:

[assembly:OwinStartup(typeof(namespace.Startup))]
public class Startup
    {
        /// <summary> Configurations the specified application. </summary>
        /// <param name="app">The application.</param>
        public static void Configuration(IAppBuilder app)
        {
            var httpConfiguration = CreateHttpConfiguration();

            app
                .UseWebApi(httpConfiguration);
        }

        /// <summary> Creates the HTTP configuration. </summary>
        /// <returns> An <see cref="HttpConfiguration"/> to bootstrap the hosted API </returns>
        public static HttpConfiguration CreateHttpConfiguration()
        {
            var httpConfiguration = new HttpConfiguration();
            httpConfiguration.MapHttpAttributeRoutes();

            return httpConfiguration;
        }
}

现在添加一个继承自ApiController的类,用RoutePrefix属性注释它,用Route + HttpGet/PutPost(表示您要的Http动词)注释动作方法,您应该可以开始了


这是我对启动/托管web应用程序的理解,因为这一切都很令人困惑。一个小总结:

1. 典型的ASP。NET:只编写在强制IIS管道的最后一步中运行的应用程序代码

2. ASP。NET with OWIN:配置一个。NET web服务器并编写应用程序代码。不再直接耦合到IIS,因此您不再被迫使用它。

3.ASP。NET Core:配置主机和web服务器以使用和编写应用程序代码。如果你的目标是。net Core,而不是完整的。net框架,那么不再强制使用。net web服务器。


现在我将更详细地介绍它是如何工作的,以及使用哪些类来启动应用程序:

典型的ASP。网

Classic ASP.NET applications have the Global.asax file as entry point. These applications can only be run in IIS and your code gets executed at the end of the IIS pipeline (so IIS is responsible for CORS, authentication... before your code even runs). Since IIS 7 you can run your application in integrated mode which integrates the ASP.NET runtime into IIS. This enables your code to configure functionality which wasn't possible before (or only in IIS itself) such as url rewriting in the Application_Start event of your Global.asax file or use the new <system.webserver> section in your web.config file.

ASP。NET with OWIN

First of all OWIN is not a library but a specification of how .NET web servers (for example IIS) interact with web applications. Microsoft themselves have an implementation of OWIN called project Katana (distributed through several different NuGet packages). This implementation provides the IAppBuilder interface you encounter in a Startup class and some OWIN middleware components (OMC's) provided by Microsoft. Using IAppBuilder you basically compose middleware in a plug-and-play way to create the pipeline for the webserver (in addition to only the ASP.NET pipeline in IIS7+ as in the point above) instead of being tied to the IIS pipeline (but now you use a middleware component for CORS, a middleware component for authentication...). Because of this, your application is not specifically coupled to IIS anymore and you can run it on any .NET Webserver, for example:

OwinHost包可以用一个Katana web服务器自托管你的应用程序。 systemweb包用于以集成模式在IIS7+中托管您的OWIN应用程序,方法是在内部将您的中间件订阅到正确的生命周期事件。

让人困惑的是环球公司。asax仍然与OWIN Startup类一起被支持,尽管它们都可以做类似的事情。例如,您可以在全局中实现CORS。asax和使用OWIN中间件的身份验证,这变得非常混乱。

我的经验法则是移除全局。asax文件,以便在需要添加OWIN时使用启动。

ASP。网络核心

ASP。NET Core是下一个进化,现在你可以瞄准。NET Core或者完整的。NET框架。当你瞄准。net Core时,你可以在任何支持。net标准的主机上运行你的应用程序。这意味着你不再局限于一个。net web服务器(如前一点),而是可以在Docker容器中托管你的应用程序,linux web服务器,IIS…

ASP的入口点。NET Core web应用程序是Program.cs文件。在这里,您配置了主机,并再次指定了配置管道的Startup类。使用OWIN(通过使用IAppBuilder。UseOwin扩展方法)是可选的,但完全支持。