ASP. Mvc--controller

Source: Internet
Author: User

The controller is responsible for controlling all client-server interactions in ASP. and is responsible for reconciling the data transfer between model and view, which is the core of ASP.

Basic requirements for writing a controller:

1, controller must be open category;

2. Controller name must end with controller;

3. You must inherit the controller class built into ASP. NET MVC, or inherit a custom category that has the IController interface, or do it yourself icontroller;

4. All action methods must be public methods, and any method that is declared private or protected is not considered an action method.

Each action in the controller can define 0~ multiple parameters, and when an action method in the controller is called, the return value is usually ActionResult or a derived class. Of course, it can also be used directly. NET built-in basic data type as a callback type, if the action is declared void, the action does not return any data to the client.

First, controller action method selected

The main purpose of nonaction is to protect certain public methods in the controller from being published to the web, or for programs that have not yet completed development and do not want to be exposed.

Action Method Selector--http verb-restricted properties: HttpGet, HttpPost, Httpdelete, Httpput, Httphead, httpoptions, these are part of the action method selector.

HttpGet: Making a request to a specific resource;

HttpPost: Submits data to the specified resource for processing requests;

Httpdelete: The request server deletes the resource identified by the Request-uri;

Httpput: Upload its latest content to the specified resource location;

Httphead: asks the server for a response that is consistent with the GET request, except that the response body will not be returned;

Httpoptions: Returns the HTTP request method that the server supports for a specific resource;

Httppatch: Submits data to the specified resource for update processing requests.

ActionResult Derived types:

Type Controller helper Method Use
Contentresult Content Callback a user-defined user attribute
emptyresult   does not respond to any information to the client

fileresult

  • Span style= "FONT-SIZE:14PX;" >filecontentresult
  • filepathresult
  • filestreamresult
File

return a document message in binary stream mode:

  • direct output byte[] property
  • Specify the stream object to return its properties

Httpstatuscoderesult

  • Httpnotfoundresult
  • Httpunauthorizeresult
Httpnotfound

Callback the custom HTTP status code and message:

  • Callback HTTP 404 Status Code
  • Callback HTTP 401 Status code
Javascriptresult Javascript

The callback is a JavaScript script

Jsonresult Json Serialization of data into JSON format callbacks
Redirectresult

Redirect

Redirectpermanent

Re-directed to the specified URL
redirecttorouteresult

redirecttoaction

Redirecttoactionpermanent

redirecttoroute

redirecttoroutepermanent

similar to Redirectresult, but it is redirected to an action or route

Viewresultbase

  • ViewResult
  • Partialviewresult

View

Partialview

Callback a View page

  • Callback View page (View page)
  • Return Part View page (partial view)

Second, controller coordinated data transfer

The controller is responsible for handling all browser requirements and deciding what properties to respond to the browser, and in addition to this, the Controller is responsible for coordinating the data transfer between the model and the view. Several ways to pass data to view in asp: ViewData, ViewBag, and TempData.

ViewData is a dictionary collection that reads the corresponding value using the key value: viewdata["name"]= "Jessica", the passed key must be a string type;

ViewBag is a dynamic type, acting like ViewData, is used to store information, there are some different viewdata is the object type, ViewBag is a dynamic type, from the operational efficiency, viewbag higher than ViewData, That is, you can enter a few words. Method of Use: Viewbag.data= "Jessica";

TempData and ViewData is the same word typical, its type is tempdatadictionary, to note that the information in the tempdata of the temporary time for a Web page request when the page is refreshed again, the data disappears. How to use: tempdata["name"]= "Jessica".

Third, Action filter

While the controller is running, there is also a layer of action filter Mechanisms: Authorization filters, action filters, result filters, and exception filters.

Authorization Filter Properties: The authorization filter is the first filter to run before the Controller and action in ASP. NET MVC, which can be used to make some additional judgments about the action before it is formally run. Asp. There are several authorization filters built into the net: authorize, AllowAnonymous, Childactiononly, Requirehttps, ValidateInput, and Validataantiforgerytoken.

Authorize: Set certain qualifying roles to run this action.

AllowAnonymous: Typically used in conjunction with authorize to set the action to run without logging in.

Childactiononly: Setting an action that runs through Renderaction only allows the Html.renderaction helper method to run.

Requirehttps: Make the action visible only on HTTPS security online.

ValidateInput: Turn off Web input validation.

Validataantiforgerytoken: An attack to prevent cross-site fraud clicks.

Action Filter Properties: provides two events before and after the action is run, respectively, OnActionExecuting and onactionexecuted. There are several action filters built into asp: Actionfilter, AsyncTimeout, and Noasynctimeout.

Actionfilter: is an abstract class, without any program code, just a basic Actionfilter program code framework, you can directly inherit ActionFilterAttribute when you customize the action filter.

AsyncTimeout: Sets the number of milliseconds to run when the asynchronous controller is running.

Noasynctimeout: Set not to have overdue time when running an asynchronous controller (unlimited overdue time).

Result Filter Properties: Provides two events to run before and after the view is run, respectively onresultexecuting and onresultexecuted.

Exception Filter Properties: Starting from the first authorization filter run, you can do further processing in the Exception Filter property if any of the exceptions occur during this period after ActionResult is finished running.

ASP. Mvc--controller

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.