如果我使用的是 OWIN Startup.cs 类并将所有配置移到那里,我是否需要 Global.asax.cs 文件? [英] Do I need a Global.asax.cs file at all if I'm using an OWIN Startup.cs class and move all configuration there?

查看:30
本文介绍了如果我使用的是 OWIN Startup.cs 类并将所有配置移到那里,我是否需要 Global.asax.cs 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在一个全新的 ASP.NET MVC 5 应用程序中,如果我删除 Global.asax.cs 类并将其配置代码移动到 Startup.cs Configuration() 方法如下,有什么缺点?

Let's say for example in a brand new ASP.NET MVC 5 application made from the MVC with Individual Accounts template, if I delete the Global.asax.cs class and move it's configuration code to Startup.cs Configuration() method as follow, what are the downsides?

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.NET 4 应用程序升级到 ASP.NET 5 并使用现在必须在 Startup.cs 类中配置的部分时,我不会在两个不同的类中进行依赖注入和其他配置这似乎与启动和配置有关.

The upsides for me is that when upgrading ASP.NET 4 applications to ASP.NET 5 and using pieces that now must be configured in the Startup.cs class, I'm not doing dependency injection and other configuration in two different classes that seem related to starting up, and configuration.

推荐答案

Startup.Configuration 的调用比 Application_Start 稍晚一些,但我认为在大多数情况下差别不大.

Startup.Configuration gets called slightly later than Application_Start, but I don't think the difference will matter much in most cases.

我认为我们在 Global.asax 中保留其他代码的主要原因是:

I believe the major reasons we kept the other code in Global.asax are:

  1. 与以前版本的 MVC 保持一致.(这是目前每个人都希望找到此代码的地方.)
  2. 能够添加其他事件处理程序.在 Global.asax 中,您可以处理其他方法,例如 Session_Start 和 Application_Error.
  3. 在各种身份验证场景中的正确性.仅当 bin 目录中有 Microsoft.Owin.Host.SystemWeb.dll 时才会调用 Startup.Configuration 方法.如果您删除此 DLL,它将自动停止调用 Startup.Configuration,这可能很难理解.

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

I think the third reason is the most important one we didn't take this approach by default, since some scenarios don't include having this DLL, and it's nice to be able to change authentication approaches without invalidating the location where unrelated code (like route registration) is placed.

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

But if none of those reasons apply in your scenario, I think you'd be fine using this approach.

这篇关于如果我使用的是 OWIN Startup.cs 类并将所有配置移到那里,我是否需要 Global.asax.cs 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆