Code:
Webapiconfig
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net.Http;usingSystem.Web.Http;usingSystem.Configuration;usingMicrosoft.Owin.Security.OAuth;usingNewtonsoft.Json.Serialization;usingSystem.Web.Http.Cors;usingSystem.Web.Http.WebHost;usingSystem.Web.SessionState;usingSystem.Web.Routing;usingsystem.web;namespaceworkordermanage.api{ Public Static classWebapiconfig { Public Static voidRegister (httpconfiguration config) {//Web API Configuration and Services//Configure the Web API to use anonymous token authentication only. CONFIG. Suppressdefaulthostauthentication (); Config. Filters.add (NewHostauthenticationfilter (Oauthdefaults.authenticationtype)); ////Web API Routing //CONFIG. Maphttpattributeroutes (); //CONFIG. Routes.maphttproute (//Name: "Defaultapi",//routetemplate: "Api/{controller}/{action}/{id}",//defaults:new {id = routeparameter.optional, namespacename = "WorkOrderManage.API.Controllers"}//); //there is already a global, if you add this route, then the global inside will be invalid//CONFIG. Routes.maphttproute (//Name: "Admin area Default",//routetemplate: "Api/admin/{controller}/{action}/{id}",//defaults:new {id = routeparameter.optional, namespacename = "WorkOrderManage.API.Areas.Admin.Controllers"} //); } } Public classSessionroutehandler:httpcontrollerhandler, IRequiresSessionState { PublicSessionroutehandler (Routedata routedata):Base(Routedata) {}} Public classSessioncontrollerroutehandler:httpcontrollerroutehandler {protected OverrideIHttpHandler Gethttphandler (RequestContext requestcontext) {return NewSessionroutehandler (Requestcontext.routedata); } }}
Add the following two classes, comment out the routes inside, add routes in the global
Global.asax
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net.Http.Formatting;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;usingSystem.Web.SessionState;usingWorkOrderManage.API.Utility;namespaceworkordermanage.api{ Public classWebApiApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (Bundletable.bundles); varJsonformatter =NewJsonmediatypeformatter (); GlobalConfiguration.Configuration.Services.Replace (typeof(Icontentnegotiator),NewJsoncontentnegotiator (Jsonformatter)); RouteTable.Routes.MapHttpRoute (Name:"Admin Area Default", Routetemplate:"Api/admin/{controller}/{action}/{id}", defaults:New{id = routeparameter.optional, namespacename ="WorkOrderManage.API.Areas.Admin.Controllers" } ). Routehandler=NewSessioncontrollerroutehandler (); RouteTable.Routes.MapHttpRoute (Name:"Email Area Default", Routetemplate:"Api/email/{controller}/{action}/{id}", defaults:New{id = routeparameter.optional, namespacename ="WorkOrderManage.API.Areas.Email.Controllers" } ). Routehandler=NewSessioncontrollerroutehandler (); } Public Override voidInit () {postauthenticaterequest+=mvcapplication_postauthenticaterequest; Base. Init (); } voidMvcapplication_postauthenticaterequest (Objectsender, EventArgs e) {HttpContext.Current.SetSessionStateBehavior (sessionstatebehavior.required); } }}
Add a route inside this
You cannot add a route in the global and add a route with the same name in Webapiconfig
Http://www.cnblogs.com/firstcsharp/p/4482835.html
WEBAPI Session Support