c# - Using WebAPI 2.2 inside of an existing ASP.NET MVC 5 project -
i using webapi 2.2, attribute routing, inside of existing mvc 5 project. intend migrate entire website on webapi, take time. got working, concerned may doing wrong.
if remove httpconfiguration argument typically provided in webapiconfig.register()
, , call globalconfiguration.configure(x => x.maphttpattributeroutes())
within webapiconfig.register()
method - webapi endpoints respond desired results.
so end with:
public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); webapiconfig.register(); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); } } class webapiconfig { public static void register() { globalconfiguration.configure(x => x.maphttpattributeroutes()); } }
is there wrong approach?
so turns out there 2 ways fix configuration issue comes when webapi 2.2 added existing project. doing both fixes became clear me when read code.
the following:
public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); webapiconfig.register(); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); } } class webapiconfig { public static void register() { globalconfiguration.configure(x => x.maphttpattributeroutes()); } }
is virtually same doing following:
public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); //webapiconfig.register(); globalconfiguration.configure(webapiconfig.register); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); } } class webapiconfig { public static void register(httpconfiguration configuration) { configuration.maphttpattributeroutes(); } }
it seems experiencing mental lapse :)
it should have been obvious globalconfiguration.configure(x => x.maphttpattributeroutes())
doing same thing globalconfiguration.configure(webapiconfig.register)
.
it logically follows these should produce same result. here microsoft code globalconfiguration in system.web.http namespace:
/// <summary> /// provides global <see cref="t:system.web.http.httpconfiguration"/> asp.net applications. /// </summary> public static class globalconfiguration { private static lazy<httpconfiguration> _configuration = createconfiguration(); ///... code excluded brevity /// <summary> /// gets global <see cref="t:system.web.http.httpconfiguration"/>. /// </summary> public static httpconfiguration configuration { { return _configuration.value; } } /// <summary> /// performs configuration <see cref="globalconfiguration.configuration"/> , ensures /// initialized. /// </summary> /// <param name="configurationcallback">the callback perform configuration.</param> public static void configure(action<httpconfiguration> configurationcallback) { if (configurationcallback == null) { throw new argumentnullexception("configurationcallback"); } configurationcallback.invoke(configuration); configuration.ensureinitialized(); } private static lazy<httpconfiguration> createconfiguration() { return new lazy<httpconfiguration>(() => { httpconfiguration config = new httpconfiguration(new hostedhttproutecollection(routetable.routes)); servicescontainer services = config.services; contract.assert(services != null); services.replace(typeof(iassembliesresolver), new webhostassembliesresolver()); services.replace(typeof(ihttpcontrollertyperesolver), new webhosthttpcontrollertyperesolver()); services.replace(typeof(ihostbufferpolicyselector), new webhostbufferpolicyselector()); services.replace(typeof(iexceptionhandler), new webhostexceptionhandler(services.getexceptionhandler())); return config; }); } ///... code excluded brevity }
Comments
Post a Comment