AP. net mvc: controller and controller Actions

Source: Internet
Author: User

ASP. NET endpoints des the following types for MVC development:

  • IController. This class defines the controller class conventions.
    Controller. This is a controller base class that provides basic MVC processing.

  • Note
    You can usePrincipalPermissionAttributeClass.

All controller classes must be suffixed with "Controller" and inherit from the "Controller" base class.

MVC framework (MVC2) considers all public methods to be action methods. Therefore, if your controller class contains a public method that is not an action method, you must mark it with the NonActionAttribute feature.

Action method parameters
By default, parameters of the action method are retrieved from the requested dataset. A data set includes data, query string, and name/value pairs in cookies.
The controller class locates the action method and determines the parameters used for the method based on the RouteData instance. If the parameter value cannot be parsed, the NULL value is passed for the reference type or NULL type. Otherwise, an exception is thrown.

There are several ways to obtain the URL parameter value in the action Method of the control class. The Controller base class exposes the Request and Response objects used in the action method. The two objects share the same meaning as HttpRequest and HttpResponse. One important difference is that the Request and Response objects in the Controller class are based on the System. Web. IHttpRequest and System. Web. IHttpResponse interfaces. This interface makes it easy for them to create a mock object, making it easy to create unit tests for the Controller class.

Process undefined Action name
The Controller base class calls the HandleUnknowAction method to process an unknown action method name. By default, the Controller base class returns an HTTP 404 status code.
You can set the default handler for the action referenced in the URL but the action method does not actually exist. The following example shows how to override the HandleUnknowAction method and redirect it to a "serch" Page:

Public void override HandleUnknownError (string action ){
// Redirect to a search page where the unknown action is
// The search query. Determine when to show the search page
// Based on the result of calling a ShouldShowSearch () method.
If (ShouldShowSearch (action) = true)
{
RedirectToAction ("search", action );
Return;
}
Base. HandleUnknownError (action );
}

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.