Session and processing method in ASP. net mvc, mvcsession

Source: Internet
Author: User
Tags asp net

Session and processing method in ASP. net mvc, mvcsession

Recently in ASP. net mvc: a Session is set in a controller, but the Session cannot be obtained in the constructor of another controller. nullReferenceException "error. This is because you want to obtain the Session value in the controller constructor and assign the value to a global variable so that multiple actions of the controller can be shared.


At first, it was thought that the ASP. NET State Service was not enabled, or the sessionState node configuration in Web. config was incorrect. None of the results were returned. The final conclusion is: in ASP. NET, the Session only exists in the action, and it is not feasible to obtain the Session in the controller constructor.

 

So how does ASP. NET Session work? In ASP. net mvc, how does action obtain a Session?

 

Why Session?

The ASP. NET page is "stateless", which means that each time a request is sent to the server, the server generates an instance of the page. However, sometimes we want to share information between different pages, such as shopping cart and user logon. Therefore, ASP. NET provides a server Session mechanism.

 

How the Session works

The Session mechanism of the server is based on the client, that is to say, the Session of the server will save the information of each client to the server memory. The specific process is as follows:
→ The client sends a request to the server
→ The server responds to the client and creates a Session and a unique Session ID for the client.
→ The Session ID is used as the key, and the Session content is used as the value. It is stored in the Session State Provider in the form of a key-value pair.
→ The client sends a second request to the server with the dedicated Session ID
→ The Session mechanism of the server extracts the content from the Session State Provider based on the Session ID of the client and returns it to the client.

 

Advantages:
● Maintain user status and information across pages
● Easy to use and can store any type
● The information of each client can be saved
● Secure and transparent

 

Disadvantages:
● Because sessions are stored in the server's memory, as client requests increase, the performance may be affected.
● In Web. conig, if the mode attribute of the sessionState node is set to "StateServer" or "SQLServer", the object stored in the Session must be marked with [Serializable]. In this way, continuous serialization and deserialization during Session storage and reading will also affect the performance.

 

Session Mode

In Web. config, The sessionState node has a mode attribute. Its attribute value indicates the Mode of the Session. Including:
● InProc
● StateServer
● SQLServer
● Custom
● Off

The setting of each Mode affects the Session State Provider used by the Session mechanism.

 

□Off

If we want to invalidate the Session:

<system.web>
    <sessionState mode="off" />
</sytem.web>

 

□Inproc

This is also the default Mode used by the ASP. NET Session mechanism. In this Mode, only data in the current application domain is saved. If the server is restarted, all Session data will be lost.

<system.web>
    <sessionState mode="InProc" timeout="30" />
</system.web>

The validity period of the Session is 30 minutes. This mode is good for small websites or small data volumes.

 

Advantages:
● Because the Session data is stored in the memory, the data is obtained very quickly.
● There is no serialization or deserialization Requirement

 

Disadvantages:
● If the application domain is discarded or restarted, Session data will be lost
● When the data volume is large, excessive memory consumption affects performance.

 

□Stateserver

If this option is selected, it means that the sessionwork is handed over to the asp.net_state.exe service outside the current application domain, which is a Windows service independent of IIS. If you want to start the Service, you can open "control panel -- Management Tools -- Service", find the Service ASP. NET State Service, and set it to auto start.

 

Even if the ASP. NET process is restarted, the Session is still valid, which is an advantage of the StateServer mode. The disadvantage of this mode is that it involves too much serialization and deserialization.

<system.web>
    <sessionState mode="StateServer" stateConnectionSting="tcpip=127.0.0.1:42424" stateNetworkTimeout="40" />
</sytem.web>

● 127.0.0.1 indicates that the local machine is selected by default.
● 42424 indicates the default port
● StateNetworkTimeout is used to set the server response time and wait for client requests. The default value is 10 seconds. The value is 40 seconds.

If you want to modify the stateserversions, you can modify it in the Registry corresponding to asp.net_state.exe.

 

□Sqlserver

In this mode, Session data is serialized and saved to the SQL Server database. To enable SQL Server to work with this mode, you need to do the following:
→ View the version location, for example, in C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319.
→ Enter the following command:


-Ssadd: adds the session state to SQL Server.
-Sstype p indicates Persisted
-S indicates the server name
-U indicates the SQL Server user name
-P indicates the SQL Server password


→ If everything goes well, the following prompt appears:


→ When an ASPState database is added to the database, Session data is saved to the relevant table of the database.

 

Advantages:
● Session data will not be affected after IIS is restarted
● The safest Session Processing Method

 

Disadvantages:
● Relatively slow processing
● Excessive serialization and deserialization

 

□Custom

A custom Session mechanism that inherits the SessionStateStoreProviderBase class to implement the ISessionIDManager interface.

 

Session Event

There are two Session events: Session_Start and Session_End. You can process these two events in the global. asax global File.

void Session_Start(object sender, EventArgs e) 
{
    //TODO
}
void Session_End(object sender, EventArgs e) 
{
    // TODO
}

 

ASP. net mvc processes sessions in two ways

To process user logon, you can use the method described in "MVC extended ValueProvider and create SessionValueProvider by implementing the IValueProvider interface. If you are processing a cart, you can use the method described in http://www.cnblogs.com/darrenji/p/3813109.html.

 

References:

Http://www.cnblogs.com/ghd258/archive/2006/02/28/339443.html
Http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net


Session usage in asp net mvc

Assign a value to the Session: Session ["session name"] = value to be assigned to the Session
Read Session value: the data type variable to be obtained = (the data type to be obtained) Session ["session name"];
 
Session usage in asp net mvc

Session ["*****"] = the value you want to assign;
In MVC, You can exchange la. text = session ["*****"];
In this way, la. text can get the value you want to assign. Before adding the value to the answer, you need to give it a value.

Related Article

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.