WEBAPI Session Support

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.