ASP. net mvc 4.0 learning 4-ActionResult, mvc4-actionresult

Source: Internet
Author: User

ASP. net mvc 4.0 learning 4-ActionResult, mvc4-actionresult

1. ControllerIntroduction

The Controller assumes the role of a data volume, controls the process, determines which Model to access, and determines which View plane to display, that is, ASP. net mvc all the tasks related to "zookeeper" are handled by the Controller.

The traffic segment of the Controller calls the data processing in the Model and sends the processing result to the View of the response.

Controller is a Class, which contains many Method methods and processes in the Method.

The Controller has the following features:

  • Type must be Public open
  • The class name must end with the Controller
  • Controller class that must inherit from ASP. NET MVC
  • The Action Method in Controller must be Public

2. The Controller completes the process

Step 1Accept request

Request that the server receives the client

Step 2Ingress route

After receiving the request, UrlRoutingModule finds the matched RouteData one by one for the Route that matches the RouteTable, and then determines the corresponding Controller and Action for the root Route number.

Step 3 MVCRequest Handling Procedure

Then the MVCRouteHandler handler creates an MVCHandler handler and sends it to RequestContext.

Step 4ZhujianController

MVCHandler uses RequestContext to confirm IcontrollerFactory to create a Controller

Step 5RowsController

MVCHandler uses the Controller to export data.

Step 6LicenseAction

Then, ControllerActionInvoker determines which Action is taken from the Controller.

Step 7RowsResult

The Action method receives the response from the client and returns the result of the response type.

 

When the Controller is selected by MVCHandler, The ActionInvoker root router selects the corresponding Action.

Ctrl + F5 run the program and we will see the first line: http: // localhost: 64570/

 

The default route configuration in root route RouteConfig. cs is as follows:

  routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }            );

Http: // localhost: 64570/the default route configuration on the root region fills in the complete Domain Name: http: // localhost: 64570/Home/Index, as a result, we can see the index's first cursor

 

3., Action Selector

3-1:Name Selector

In the operation case, the selected Action by the designated name selector will get the Index in HomeController:

Public ActionResult Index () {ViewBag. Message = "modify this notebook to start your ASP. net mvc application. "; Return View ();}

Now we add a new ActionName to the Action method and add a new View for the corresponding Action, as shown below:

// Add ActionName [ActionName ("NewActionName")] public ActionResult Index () {ViewBag. message = "modify this notebook to start your ASP. net mvc application. "; Return View ();}

After the operation is performed, the primary account cannot be found at http: // localhost: 64570/home/index.

Modify the requested Action address to: http: // localhost: 64570/home/NewActionName. Normally, the View plane added only for our workshop is displayed:

 

3-2:Method Selector

1. NonAction adequacy

In Asp.net MVC, all the public open methods in the Controller are converted to Action. If we want to create a non-Action method in the Controller that includes the public open nature, you can remember this method through the NonAction annotation:

[NonAction] public ActionResult Function () {ViewBag. Message = "modify this notebook to start your ASP. net mvc application. "; Return View ();}

2. Http Action

After adding the Http Action adequacy before the Action method, different Http requests (HttpPost, HttpGet, HttpHead, HttpPut...) will jump to different Action methods.

For example, if [HttpPost] is added before the Index, only the post request will jump to this Action.

// [HttpPost] public ActionResult Index () {ViewBag. Message = "modify this notebook to start your ASP. net mvc application. "; Return View ();}

 

3-3: ActionResult

ActionResult is the numeric type of the Action response row that returns a response. The return value is Void or String. The return types of ActionResult are as follows:

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using System. text; using System. IO; namespace MvcApplication3.Controllers {public class HomeController: Controller {public ActionResult Index () {ViewBag. message = "modify this notebook to start your ASP. net mvc application. "; Return View () ;}// add ActionName // [ActionName (" NewActionName ")] // public ActionResult Index () // {// ViewBag. message = "modify this notebook to start your ASP. net mvc application. "; // Return View (); // Add ActionName // [NonAction] // public ActionResult Function () // {// ViewBag. message = "modify this notebook to start your ASP. net mvc application. "; // Return View (); // [HttpPost] // public ActionResult Index () // {// ViewBag. message = "modify this notebook to start your ASP. net mvc application. "; // Return View (); //} // ----------------------------------- the following are the return types of the Action method -------------------- // 1, ContextResult, return the plain text file // public ContentResult About () // {// return Content ("this is the returned Content of ContentResult"); //} // 2, emptyResult returns Response null // public EmptyResult About () // {// return null; //} // 3, RedirectResult, which corresponds to Response. redirect ingress connects to the new ingress // public RedirectResult About () // {// you can link to an external webpage based on the URL /// /Return Redirect ("~ /Home/Index "); // return Redirect (" http://www.baidu.com "); //} // 4, RedirectToRouteResult, the root route is directed to // public RedirectToRouteResult About () /// {// only the Controller Action/return RedirectToAction ("Index", "Home", null) under a case can be reported; //} // 5, viewResult returns a response result // public ViewResult About () /// {//// the View to be written to can be selected from the parent board or set to the face without a template. // return View (); //} // 6. PartialViewResult is returned to partial upload, And the partition surface of the partial template cannot be added. // public PartialViewResult About () // {// return PartialView (); ///} // 7. Authorized users // public HttpUnauthorizedResult About () // {// return new HttpUnauthorizedResult (); //// when the unauthorized token is configured in webConfig, the Url is re-directed, /// <authentication mode = "Forms"> /// <forms loginUrl = "~ /Account/Login "timeout =" 2880 "/> /// </authentication> //} // 8, HttpNotFoundResult // public HttpNotFoundResult About () /// {// return HttpNotFound ("Page No Found"); //} // 9, including criptresult. In this case, the content of the returned text is known as Response. contentType is defined as application/x-javascript // public JavaScriptResult About () // {// string js = "alert (\" this is the final result of JavaScriptResult return \") "; // return JavaScript (js); //} // 10, JsonResult, ASP. net mvc change R Esponse. contentType is defined as application/json // and the object of the callback is serialized as a Json string through JavaScriptSerializer. // public JsonResult About () // {// var jsonContent = new {// Id = 1, // Text = "this is the content of JsonResult" //}; // return Json (jsonContent, JsonRequestBehavior. allowGet); //} // 11, FilePathResult, send the upload case to the client through the previous route // public FilePathResult About () // {// The MapPath method of the Server generates the specified route entry. // var imagePath = Server. mapPath (".. /Images/example.jpg" ); /// Return to the upload callback case // return File (imagePath, "image/jpeg"); //} // 12, FileContentResult, in the example, convert the string into metadata, then upload the File method and perform restore // public FileContentResult About () // {//// use Encoding to UTF8 encoded bytes. // byte [] data = Encoding. UTF8.GetBytes ("FileContentResult example"); // File) // return File (data, "text/plain", "example.txt"); //} // 13. FileStreamResult is returned to the internal content of the example, which is imported through Stream. The Server ath method of the Server is used to generate the url of the specified scheme in the example of rows, then, the FileStream into the File method in the initial example is used to restore the File. // public FileStreamResult About () // {//// fetch the route of the traffic statement. // var path = Server. mapPath (".. /Images/example.txt "); //// use FileStream to open the upload case // var fileStream = new FileStream (path, FileMode. open); // File method back to callback // return File (fileStream, "text/plain", "example.txt"); //} // 14, ActionResult, all of the above types are directly or indirectly bound to the ActionResult, so the above methods can be used on the ActionResult/ /The result returned by ActionResult is more extensive than that returned by others. // public ActionResult About () // {// if (new Random (). next () % 2 = 0) // {// return response ViewResult // return View (); //} // else // {// return response JsonResult // return Json (null, JsonRequestBehavior. allowGet); //} public ActionResult Contact () {ViewBag. message = "your zookeeper plane. "; Return View ();}}}

 


How to further learn about aspnet (MVC)

You are a newbie to learn. NET
Learn MVC again
If you are a newbie to learn. NET and a newbie to learn mvc, it may be different.
If you want to learn. Net to the framework and design mode stage
We recommend that you learn MVC
If not. Net

Differences between several controllers in aspnet MVC40

Www.cnblogs.com/..w.html
 

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.