ASP. NET MVC part45 filter, template page

Source: Internet
Author: User

Filter filters

How to use
The custom class inherits from the corresponding class or interface, overriding the method, using as an attribute
Overriding methods in the Controller class

Use of the attribute mode
Note: If inheriting from an interface requires the class to implement FilterAttribute, it can be used as an attribute
How to use 1: as a controller or action feature
Use Mode 2: Register as Global filter in global, apply to all Controller's action
The parameter classes are inherited from ControllerContext, which mainly contain the property request context, routing data, results

Authentication Filters
Execute before the action of the constraint is executed
Overriding the Onauthorization method
If you do not want to use the default authentication method, you need to remove base.***
If you want to jump, you need to set the context object's Result property to New Redirectresult (string URL);

Exception Handling Filters
Used for custom asynchronous processing when an exception occurs, such as logging, jump page
Using custom exception handling, you need to add <customerrors mode= "on"/> nodes to system.web in Web. config
Override the Onexception method, and do not disable base.***
If you want to jump, you need to set the context object's Result property to New Redirectresult (string URL);

Behavior Filter
Filters that are executed before and after the behavior is executed
overriding method OnActionExecuting (before behavior execution)
overriding method OnActionExecuted (after the execution of the behavior)

Result Filter
Filters that are executed before and after the result is executed
overriding method onresultexecuting (before result execution)
overriding method onactionexecuted (after result execution)

Template page
Templates page: Layout on one page and leave blank areas for child pages to fill
In the MVC project, there is a shared folder under the Views folder to store all the template pages
View engine, default first find ASPX engine page, find Razor engine page if not present

Using master page Master for the ASPX engine
Create a new master page, use ContentPlaceHolder to empty multiple areas for child page fills, set the id attribute to differentiate
Create a new ASPX engine page for action, select the master page and specify the ID value of the primary fill area
Code that writes specific content in the corresponding content tag, identifying which area of the master page is replaced by the ContentPlaceHolderID property

For the razor engine, use the layout page. cshtml
Create a new MVC layout page in the shared folder
Use @renderbody () to identify blank areas
Create a new Action view, select the layout page you just created, and the property layout points to the address of the layout page
Write a piece of code on the page and run it to see the effect
If you do not specify layout, the template page specified by _viewstart.cshtml is used, which is the part of all page calls
If you do not want to use the template page, set layout= ""
You can use Rendersection (string name, bool required) to key out more areas in a layout page, use the @section name {...} in a child page. To fill

ASP. Part45 filter, template page

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.