The Session value problem of MVC4 Controller and WebApi

Source: Internet
Author: User
Tags httpcontext

After MVC, the session may be less common, but occasionally used, such as page verification code. For example, the verification code used by the login page is implemented by a controller that provides a view, and the session can be used to store the value. But now the commonly used requests can be handed over to Webapi to handle, according to the default way to set up the MVC4 application, Webapi is not to be saved by the controller session value, then we need to make certain configuration. The records are as follows:

Step one: Simply set up two classes to modify the route of the WEBAPI

The first class Sessionroutehandler, inherits from Httpcontrollerhandler, and implements IRequiresSessionState interface at the same time, In fact, there is no internal method of irequiressessionstate, so there is no need to write anything.

1  Public class  sessionroutehandlerhttpcontrollerhandler,irequiressessionstate  2 {3          Public Sessionroutehandler (routedatabase(Routedata)4        { 5         }6 }

Second class Sessioncontrollerroutehandler, inherited from Httpcontrollerroutehandler

1  Public class sessioncontrollerroutehandler : httpcontrollerroutehandler 2 {3         protected Override IHttpHandler gethttphandler (requestcontext requestcontext)4         {5             return New Sessionroutehandler(requestcontext.routedata);6         }7}

After you complete these two classes, you are ready for the next step.

Step Two: Modify the Webapiconfig, give the novice a hint, this class can be seen in global, webapiconfig.register (... It's in the making. In general, under the App_start directory. Since we are going to allow WEBAPI to get the session set in the controller in MVC, we need to change this configuration again.

1  Public Static classWebapiconfig2 {3      Public Static voidRegister (httpconfiguration config)4     {5             //CONFIG. Routes.maphttproute (6             //Name: "Defaultapi",7             //routetemplate: "Api/{controller}/{id}",8             //defaults:new {id = routeparameter.optional}9             //);Ten             //Pass session on Route One RouteTable.Routes.MapHttpRoute ( AName"Defaultapi", -Routetemplate:"Api/{controller}/{id}", -DefaultsNew{id = routeparameter.optional}). Routehandler =NewSessioncontrollerroutehandler (); the     } -}

This is directly configured from routetable and specifies the Sessioncontrollerroutehandler that Routehandler set for our first step. But the work has been done, the session will not be successful delivery, because here we have to specify the behavior.

Step three: Modify Global.

In the CS code of Global, overload its Init method with the following code:

1  Public Override void Init () 2 {3      Postauthenticaterequest + = (s, e) = HttpContext.Current.SetSessionStateBehavior ( sessionstatebehavior.required); 4      Base . Init (); 5 }

Well, you can compile it when you're done, and then you can set up the session in the Controller in the normal way, for example:

session["validcode"]="Session Test "

Then you can get it in Webapi:

httpcontext.current.session["validcode"]. ToString ()

End of this article

The Session value problem of MVC4 Controller and WebApi

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.