ASP. net mvc controller and action

Source: Internet
Author: User
1. The following two methods can be used to obtain the parameters and values of this request in the Action Method: 1.1 is the most direct method, which is obtained from the context object. Generally, our controller classes are inherited from system. web. MVC. controller class. This parent class has several objects that can be directly used in our controller class, such as request, response, routedata, httpcontext, server, and session. Below are some common cases

Attribute Type Description
Request. querystring Namevaluecollection Obtain the variable and value of the query string from this GET request.
Request. Form Namevaluecollection Get the variable and value from this POST request
Request. Cookie Httpcookiecollection Cookie value sent by the browser in this request
Request. Uri Uri Requested URL path
Routedata. Values Routevaluedictionary Route Parameters
Httpcontext. Session Httpsessionstatebase Get the key value in the session
User Iprincipal Obtains the authorization information of the current user.
We will use this in action

PublicActionresult index (){StringInfo = request. querystring ["Info"]; Viewbag. Info=Info;ReturnView ();}
1.2 obtain from the parameter of the action. When processing a request, the MVC Framework automatically assigns values based on the parameter name from the collection of request. querystring, request. Form, request. routedata. values, and so on, These parameters cannot contain the out and ref keywords, and are case insensitive.   .

PublicActionresult index (StringInfo) {viewbag. Info=Info;ReturnView ();}
After this process, the data provider first retrieves the value from the preceding geometric objects and assigns it to the data Binder for verification and type conversion, the parameter value assigned to the action can be of any type. net type. The binding method will be later Article . If Model binder cannot assign a value to a parameter, a default value may be assigned to this value (the default value type is different from the default value of the reference type), and an exception may be thrown. To avoid this, You can manually assign the default value to the action parameter, as shown in the following figure. Code As shown in:
PublicActionresult index (StringInfo ="Hello, world!") {Viewbag. Info=Info;ReturnView ();}
If the MVC framework fails to assign values to the info variable, the default value is "Hello, world !". 2. Multiple actionresult objects are returned. After the action method is executed, data is returned to the requested user. We can let the action method return HTML or response. Redirect to a new URL. However, this method is inefficient in writing code and is not easy to maintain, which is not conducive to unit testing. We can simply return an actionresult object.

type description method name
viewresult display the specified or default view template View
partialviewresult display the specified or default partial Page Template partialview
redirecttorouteresult Redirection

redirecttoaction, redirecttoactionpermanent,

redirecttoroute, redirecttoroutepermanent

redirectrestult Redirection redirect, redirectpermanent
contentresult Returns plain text information content
fileresult return the binary file to the browser file
jsonresult serialize A. Net object into a JSON string and return it JSON
httpunauthorizedresult HTTP 401 is returned
httpnotfountresult the specified file 404 is not found httpnotfound
3. Return viewresult. To transfer data from an action to a view, you can pass a model object to the view in the following three ways: 3.1. The view receiving data should be a strong view.

 
PublicActionresult about () {datetime date=Datetime. now;ReturnView (date );}
View page call method @ model datetime @ model. Month 3.2 transmits data to the view through viewbag. Because viewbag is of the dynamic type, we can assign any attribute value to viewbag.
 
Public actionresult about ()
{Viewbag. Date=Datetime. Now; viewbag. Message="Hello, world !";ReturnView ();}
The call method of the view page @ viewbag. Date @ viewbag. Message 3.3 transmits data to the view through viewdata.

PublicActionresult about () {viewdata ["Date"] =Datetime. Now; viewdata ["Message"] ="Hello, world !";ReturnView ();}

 

Call method of the view page (forced type conversion is required) @ viewdata ["date"] @ viewdata ["message"] 4. The redirectresult object is returned. Redirectresult can be divided into two types: the first is the normal jump, and the second is the permanent jump, that is, in the browser through the "back" is not returned, the method names for this jump end with "permanent", such as redirecttoactionpermanent and redirecttoroutepermanent. The methods can be used based on the overloaded parameters. We will not introduce them here. 5. Return the contentresult object. If you want to return text information, such as XML, HTML, or plain text information, you can return the contentresult object. Return content ("this is content", "text/plain", system. Text. encoding. Default); 6. Return the jsonresult object. JSON is a popular cross-platform plain text format. It is generally used in Ajax technology. Return JSON (jsondata); 7. Return the fileresult object. It is generally used to provide file downloads. Return file ("FILENAME", "contenttype", "downloadname"); 8. Return the error message or return the HTTP code return New httpstatuscoderesult (404, "url not found... "); Or return the encapsulated method return httpnotfound ();
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.