将 Global.asax 迁移到 Startup.cs

33

为了更好地使用Microsoft.Owin.Testing.TestServer进行测试工作,我发现Owin TestServer没有加载Global.asax。

因此,我尝试将我的Global.asax配置移动到Startup.cs中,如下所示:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // pasted Global.asax things start.
        GlobalConfiguration.Configuration.Formatters.Clear();

        var jsonSerializerSettings = new JsonSerializerSettings
        {
            PreserveReferencesHandling = PreserveReferencesHandling.Objects,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        };
        GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = jsonSerializerSettings });
        GlobalConfiguration.Configuration.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());

        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        // pasted Global.asax things end.

        ConfigureAuth(app);
    }
}

但是TestServer在配置的每个点,例如AreaRegistration.RegisterAllAreasFilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)等,都未能初始化,因此失败了...

对我而言,最小可行的迁移(使用TestServer成功测试)如下所示。

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        config.Formatters.Clear();

        var jsonSerializerSettings = new JsonSerializerSettings
        {
            PreserveReferencesHandling = PreserveReferencesHandling.Objects,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        };
        config.Formatters.Add(new JsonMediaTypeFormatter() { SerializerSettings = jsonSerializerSettings });
        config.Formatters.Add(new FormMultipartEncodedMediaTypeFormatter());

        WebApiConfig.Register(config); // moved from GlobalConfiguration.Configure(WebApiConfig.Register)
        app.UseWebApi(config);
        ConfigureAuth(app);
    }
}

有没有办法将所有配置移动到Startup.cs中?

1个回答

49

如您所知,由Startup.Configuration()使用的OwinContext与由MvcApplication.Application_Start()使用的传统ASP.NET HttpContext不同。两者使用不同的上下文管道。更具体地说,ASP.NET MVC仍然依赖于System.Web.dll,而ASP.NET Web API则没有。

因此,根据您的代码,一些通常在MvcApplication.Application_Start()中的方法无法在Startup.Configuration()中运行:

  • AreaRegistration.RegisterAllAreas();:此方法依赖于System.Web.dll
  • RouteConfig.RegisterRoutes(RouteTable.Routes);RouteCollectionSystem.Web.dll的一部分。
  • GlobalConfiguration.Configure(WebApiConfig.Register):同样,在WebApiConfig.Register()中的RouteCollectionSystem.Web.dll的一部分。

对于OWIN上下文中的URL路由,建议使用AttributeRouting。因此,尝试使用config.MapHttpAttributeRoutes();。这将为您带来更大的自由度。

如果您仍然想在OWIN上下文中运行AreaRegistration.RegisterAllAreas();Startup.Configuration(),我更好地建议您导入Katana库。这将将OWIN与System.Web.dll集成在一起,以便您可能实现目标。

希望对您有所帮助


1
那我们只需要安装这个软件包就可以了吗?还是还有其他的事情要做? - Gurpreet
4
@Gurpreet 确实。如果你至少安装了Microsoft.Owin.Host.SystemWeb并将Global.asax.cs中的某些部分从Application_Start移动到Startup.cs,那么就应该没问题了。 - justinyoo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接