MVC4 WEBAPI Development If you want to support the session, please do the following several aspects of the problem

Source: Internet
Author: User

1. Establish Httpcontrollerhandler and Httpcontrollerroutehandler in Webapiconfig and overwrite it

public class Sessionroutehandler:httpcontrollerhandler,irequiressessionstate        {public            Sessionroutehandler (Routedata routedata)                : Base (routedata)            {            }        } public        class Sessioncontrollerroutehandler: Httpcontrollerroutehandler        {            protected override IHttpHandler Gethttphandler (RequestContext requestcontext)            {                return new Sessionroutehandler (Requestcontext.routedata);            }        }

Routes.maphttproute to be modified to the following format:

public static void Register (httpconfiguration config)        {            routetable. Routes.maphttproute (                name: "Defaultapi",                routetemplate: "Api/{controller}/{action}/{id}",                defaults: New {id = routeparameter.optional}            ). Routehandler = new Sessioncontrollerroutehandler ();        }

2. In MVC Webapi, the session support is not enabled by default. You need to override the Init method in global to specify the types that the session needs to support

        public override void Init ()        {            postauthenticaterequest + = mvcapplication_postauthenticaterequest;            Base. Init ();        }        void Mvcapplication_postauthenticaterequest (object sender, EventArgs e)        {            HttpContext.Current.SetSessionStateBehavior (sessionstatebehavior.required);        }

MVC4 WEBAPI Development If you want to support the session, please do the following several aspects of the problem

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.