Migrate Global.asax to Startup.cs

Youngjae picture Youngjae · Jul 30, 2014 · Viewed 31k times · Source

For better test job with Microsoft.Owin.Testing.TestServer, I found that Global.asax is not loaded with Owin TestServer.

So, I try to move my Global.asax configurations to Startup.cs as below,

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);
    }
}

But TestServer failed to initialize in every point of configuration such as AreaRegistration.RegisterAllAreas, FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters), so on...

Minimum viable migration(successful test with TestServer) for me is as below.

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);
    }
}

Is there anyway to move all configurations to Startup.cs?

Answer

justinyoo picture justinyoo · Aug 1, 2014

As you are already aware, OwinContext consumed by Startup.Configuration() is different from the traditional ASP.NET HttpContext consumed by MvcApplication.Application_Start(). Both are using different context pipelines. More specifically, ASP.NET MVC still relies on System.Web.dll while ASP.NET Web API doesn't.

Therefore, based on your code, some methods usually laid in MvcApplication.Application_Start() can't be run within Startup.Configuration():

  • AreaRegistration.RegisterAllAreas();: This method relies on System.Web.dll.
  • RouteConfig.RegisterRoutes(RouteTable.Routes);: RouteCollection is a part of System.Web.dll.
  • GlobalConfiguration.Configure(WebApiConfig.Register): Again, RouteCollection within WebApiConfig.Register() is a part of System.Web.dll.

For URL routing within OWIN context, AttributeRouting is recommended. So, instead of this, try config.MapHttpAttributeRoutes(); That will give you much freedom.

If you still want to run AreaRegistration.RegisterAllAreas(); within OWIN context, Startup.Configuration(), I'd better recommend to import Katana library. This integrates OWIN with System.Web.dll so that you probably archieve your goal.

HTH