ASP. net mvc Tip #15-pass browser cookies and server variables as action parameters
ASP. net mvc Tip #15-pass browser cookies and server variables as action parameters
Original American: http://weblogs.asp.net/stephenwalther/archive/2008/07/08/asp-net-mvc-tip-15-pass-browser-cookies-and-server-variables-as-action-parameters.aspx
Http://www.cnblogs.com/mike108mvp
Note: This is my first translation. If the translation content is incorrect or inappropriate, please criticize and correct it. Thank you.
In this tip, I will demonstrate how to pass browser cookies and HTTP server variables to the Action Method of the controller, using form and query string) the same method of parameters.
In this tip, I demonstrate how you can pass browser cookies and HTTP server variables to Controller action methods in the same way as you can pass form and query string parameters.
Assume that you have an ASP. net mvc Web ApplicationProgramMake the following browser request:
Imagine that you make the following browser request against an ASP. net mvc web application:
Http: // localhost/product/Index
When you make this request, the ASP. net mvc Framework will call the index () Action Method in the productcontroller class by default. The ASP. net mvc framework has a controlleractioninvoker class that calls the action of the Controller to respond to browser requests.
When you make this request, by default, the ASP. net MVC Framework will invoke an action named index () exposed by a class named productcontroller. there is a class in the ASP. net MVC Framework, named the controlleractioninvoker class, which is responsible for invoking a controller action in response to a browser request.
The controlleractioninvoker class has some responsibilities. This class must find a method that matches the called method. In addition, controlleractioninvoker is responsible for constructing the parameter list and passing it to the called method.
The specified class has several responsibilities. This class must find a method that matches the method being invoked. Furthermore, the controlleractioninvoker is responsible for building the list of parameters that are passed to the invoked method.
For exampleCodeIn list 1, the details () controller action is called:
Imagine, for example, that the details () controller action in Listing 1 is being invoked:
Listing 1-productcontroller. CS (C #)
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. MVC;
Namespace Tip15.controllers
{
Public Class Productcontroller: Controller
{
Public Actionresult details ( Int Productid)
{
ReturnView ();
}
}
}
The details () controller Action has only one productid parameter. Controlleractioninvoker tries to get the value of this parameter from three places.
The details () controller action has a single parameter named productid. The controlleractioninvoker attempts to retrieve the value of this parameter from 3 places:
1. Explicit additional parameters passed to invokeaction ()
2. value obtained from Route Data
3. Request Value
1. Explicit extra parameters passed to invokeaction ()
2. values from the route data
3. Request values
first, try to obtain the value of the productid parameter from the parameter passed to the controlleractioninvoker. invokeaction () method. This method is called by the Controller. Execute () method. The default controller does not pass any additional parameters. If you create your own controller, you can pass any parameters you like.
first, an attempt is made to retrieve the value of the productid parameter from the parameters passed to the controlleractioninvoker. invokeaction () method. this method is called by the Controller. execute () method. the default controller does not pass any additional parameters. if you create your own controller, then you can pass any parameters that you please.
Second, try to get the value of the productid parameter from route data. Route data may contain the default value of productid. Alternatively, your own route table may map part of the URL address to the value of the productid parameter. The default route table does not map any value to the productid parameter.
Second, an attempt is made to retrieve the value of the productid parameter from the route data. the route data might contain a default value for productid. or, your route table might map a segment of a URL to the productid. the default route table does not map anything to the productid parameter.
Finally, we try to get the value of the productid parameter from the httprequest object (httpcontext. Request. The httprequest object represents the query string, form, cookie, and server variable items (in this Order ). This means that you will try to get the value of the productid parameter from all these sources.
Finally, an attempt is made to retrieve the value of productid from the httprequest object (httpcontext. request ). the httprequest object represents query string, form, cookie, and server variable items (in that order ). this means that an attempt will be made to retrieve the value of productid from all of these sources.
If controlleractioninvoker cannot obtain a parameter value from the preceding three places, controlleractioninvoker checks whether the parameter value is allowed to be null. If a parameter is allowed to be null, actioninvoker will pass a null value. Otherwise, controlleractioninvoker will throw an invalidoperationexception ). (See Figure 1)
If the controlleractioninvoker cannot retrieve the value of a parameter from these three places then the controlleractioninvoker checks whether or not the parameter can be null. if a parameter can be null, then the actioninvoker passes a null value. otherwise, the controlleractioninvoker throws an invalidoperationexception (see figure 1 ).
Figure 1-unmatched Parameters
Figure 1-can't match Parameters
It is not until I review the controlleractioninvoker class that I know that the parameters of the controller action method can be obtained from browser cookies and HTTP server variables. In code list 2, let's look at the index () Action Method in the homecontroller class.
Until I investigated the controlleractioninvoker class, I did not realize that controller action parameters cocould be retrieved from browser cookies and HTTP server variables. Consider the index () Action exposed by the homecontroller in Listing 2.
Listing 2-homecontroller. CS (C #)
Namespace Tip15.controllers
{
Public Class Homecontroller: Controller
{
Public Actionresult index ( String Http_user_agent, String Mycookie)
{< br> viewdata [ " http_user_agent " ] = http_user_agent;
viewdata [ " mycookie " ] = mycookie;
return View ();
}
Public Actionresult createcookie ( String Cookievalue)
{
VaR newcookie = New Httpcookie ( " Mycookie " , Cookievalue );
Newcookie. Expires = Datetime. Now. adddays ( 10 );
Response. appendcookie (newcookie );
Return Redirecttoaction ( " Index " );
}
}
}
The index () method receives two parameters: http_user_agent and mycookie. Because the http_user_agent parameter is consistent with a server variable, it can automatically obtain a value. Because mycookie is consistent with the browser cookie, it has a value (if the cookie has been set ). When the index () method is called, you can see the view in Figure 2.
The index () method accepts two parameters named http_user_agent and mycookie. because the parameter name http_user_agent corresponds to a server variable, this parameter gets a value automatically. because mycookie corresponds to a browser cookie, this parameter also has a value (if the cookie is set ). when the index () method is invoked, you get the view in Figure 2.
Figure 2-The index View
In fact, you can pass server variables and cookies to the Controller, which is very useful in various scenarios. For example, you may want to return different content according to the current browser type when requesting the browser. You can use the http_user_agent server variable to type the browser in your own action method and return different content, or you can return content of different language versions based on the browser's language settings.
the fact that you can pass server variables and cookies to Controller actions can be useful in a variety of different scenarios. for example, you might want to return different content depending on the type of browser being used to make the request. you can take advantage of the http_user_agent server variable in your action method to detect the type of browser and return different content. or, you might want to return messages in different spoken ages depending on the user's browser language settings.
It is also useful to pass cookie parameters. You can store a user's personalized preferences in cookies and retrieve the data from the action method. For example, you can store a user's nickname in a browser cookie and automatically retrieve the user's nickname in each controller's action method.
Passing cookie parameters also can be useful. you cocould store a user's preferences in a cookie and retrieve those preferences in an action method automatically. for example, you can store a user's nick name in a browser cookie and retrieve that nick name in every controller action automatically.
Download DEMO code
Download the code