003.asp.net MVC Centralized Management session

Source: Internet
Author: User

Original link: Http://www.codeproject.com/Tips/790387/Session-in-ASP-NET-MVC

1. Preface

Today gains Ah, see this article, concentrate on the essence of it

2. Generally in Web applications, the use of the session may have the following purposes

A. Check if the user is logged in

B. Saving permission information

C. Saving temporary data

Sometimes we change the object that the session saves, and now we try to centrally manage the session in a controller so that it can be applied to other controllers to achieve the effect of reuse.

3. Before we begin, let's consider the following scenario

A. If you want to use a session, and this session is used throughout the page, this will be a good trick

B. Redirect to a login page if a controller depends on whether seesion is null

C. Not all controllers are dependent on the session, such as Logoncontroller and Errorcontroller, these controllers are originally because there is no session value, to be different from the B case

4. Okay, start code.

A basic controller in our program, which concentrates on the types of information we want to keep in sesion in Session,tsource, can be implemented using two methods

A. If a controller is dependent on the session, then do not inherit

B. If a controller is dependent on the session, we are going to inherit

1  Public classApplicationcontroller<tsource>: Controller2     {3         Private Const stringLogonsession ="logonsession";4         Private Const stringErrorcontroller ="Error";5         Private Const stringLogoncontroller ="LogOn";6         Private Const stringLogonaction ="LogOn";7 8         protectedApplicationcontroller ()9         { Ten         } One  A         protected Override voidInitialize (RequestContext requestcontext) -         { -             Base. Initialize (RequestContext); the             if(! Isnonsessioncontroller (RequestContext) &&hassession ()) -             { - Rederect (RequestContext, Url.action (Logonaction, Logoncontroller)); -             } +         } -  +         Private BOOLIsnonsessioncontroller (RequestContext requestcontext) A         { at             varCurrentcontroller = -requestcontext.routedata.values["Controller"]. ToString (). ToLower (); -             varNonsessioncontroller = -                 Newlist<string>() {errorcontroller.tolower (), Logoncontroller.tolower ()}; -             returnNonsessioncontroller.contains (currentcontroller); -         } in  -         Private voidRederect (RequestContext RequestContext,stringaction) to         { + requestContext.HttpContext.Response.Clear (); - RequestContext.HttpContext.Response.Redirect (action); the requestContext.HttpContext.Response.End (); *         } $ Panax Notoginseng         protected BOOLhassession () -         { the             returnSession[logonsession]! =NULL; +         } A  the         protectedTSource Getlogonsessionmodel () +         { -             return(TSource) This. Session[logonsession]; $         } $  -         protected voidSetlogonsessionmodel (TSource model) -         { theSession[logonsession] =model; -         }Wuyi  the         protected voidabandonsession () -         { Wu             if(Hassession ()) -             { About Session.Abandon (); $             } -         } -}

5. End (Own)

Although this article is a skill, but I seldom use this, but he in planning these methods, there should be a bigger picture, this is where I want to learn. Of course, the session is not only used in this situation, it is possible to save some of their own things.

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.