Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate Global.asax to Startup.cs

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?

like image 956
Youngjae Avatar asked Jul 30 '14 08:07

Youngjae


People also ask

What is the difference between startup CS and global ASAX?

Global. asax file contains mostly application level pre-defined events where Startup. cs file is more about registering services and injection of modules in HTTP pipeline.

Can we use global ASAX in .NET core?

Global. ASP.NET Core introduced a new mechanism for bootstrapping an app. The entry point for ASP.NET applications is the Global. asax file. Tasks such as route configuration and filter and area registrations are handled in the Global.

How do I add global ASAX Cs to my website?

How to add global. asax file: Select Website >>Add New Item (or Project >> Add New Item if you're using the Visual Studio web project model) and choose the Global Application Class template. After you have added the global.

What is Application_Start in global ASAX?

The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.


1 Answers

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

like image 108
justinyoo Avatar answered Sep 21 '22 18:09

justinyoo