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 onSystem.Web.dll
. -
RouteConfig.RegisterRoutes(RouteTable.Routes);
:RouteCollection
is a part ofSystem.Web.dll
. -
GlobalConfiguration.Configure(WebApiConfig.Register)
: Again,RouteCollection
withinWebApiConfig.Register()
is a part ofSystem.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
Youngjae
A humble C# lover, AI-based service developer, acquired education start-up programmer, and highschool mathematics teacher. LINE Corp., Technical Fellow, 2018~. Bapul CTO, 2014~2017. acquired to LINE corporation Microsoft Azure MVP, 2014~Present. profile Please contact me at anytime if you have any question :) My personal contact can be found at GitHub.
Updated on July 06, 2020Comments
-
Youngjae almost 4 years
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 asAreaRegistration.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?
-
Gurpreet almost 9 yearsSo do we just install the package and job done or anything else have to be done ?
-
justinyoo almost 9 years@Gurpreet Indeed. If you install at least Microsoft.Owin.Host.SystemWeb and move bits and pieces from
Application_Start
inGlobal.asax.cs
toStartup.cs
, it should be fine.