Migrate Global.asax to Startup.cs

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

Leave a Comment