ASP. net mvc 3.0 Learning Series-Controllers in ASP. net mvc 3.0

Source: Internet
Author: User

Series of articles

ASP. net mvc 3.0 Learning Series-Preface

ASP. net mvc 3.0 Learning Series -- Razor and ASP. net mvc 3.0

ASP. net mvc 3.0 Learning Series-Controllers in ASP. net mvc 3.0

ASP. net mvc 3.0 Learning Series-Model in ASP. net mvc 3.0

ASP. NET MVC
3.0 Learning Series-Dependency Resolution in ASP. net mvc 3.0

This section contains the following content:

1. Introduction

In ASP. net mvc 3.0, the newly added functions of Controller mainly include ActionFilter and ActionResult. I personally think there are not many changes. In addition, the modification of the Controller part is not the highlight of ASP. net mvc 3.0.

2. Global Action Filters

In earlier versions, the concept of Action Filter exists. By default, it comes with the following Action filters.

 

 

 

These attributes can be added to an Action or the entire Controller:

Why add Global Action Filter in ASP. net mvc 3.0?

Sometimes, we add an Action before or after an Action is run. For example, after logon authentication is complete, we need to record the logon time of this person, so ASP. the net mvc team added the Action Filter function in MVC 2.
Action filters are m attributes that provide a declarative means to add pre-action and post-action behavior to specific controller action methods.
However, you may need to run some code before all actions are executed. Therefore, MVC 3 provides a new function, this allows you to add Global filter to the GlobalFilters collection.

Here is an example:

We define a CustomActionFilter that inherits the ActionFilterAttribute class.

The Custom Action Filter can be used to override four methods. When an Action is executed, after the Action is executed, when the Result is returned, after the Result is returned.

Because there is a Global Filter added in ASP. net mvc 3.0, we can now register this Filter in Global. asax.

 

The above Action Filter is not added to any Action. Run the following command:

When the action of our HomeControllerhome is run, the Custom Action we defined is executed.

3. Caching Child Actions

In ASP. net mvc 3.0, we can use the OutputCache attribute in the Child Action.

First, let's define a ChildAction:

Add a View named CurrentTime"

Because the model type is Dynamic, we only need to use @ model DateTime to define the mode.

Add the Partical view to the view of the Index as follows:

After running:

The following describes how to use the Child Cache attribute to check whether two outputs have the same time:

Running result:

 

4. The ViewBag

This is already described in the previous article.

5. New Action Results

There is a new Action Result in ASP. net mvc 3.0:

A. HttpNotFoundResult

B. HttpRedirectResult

C. HttpStatusCodeResult

 

6. Request Validation

 

 

 

A. Use the ValidateInput attribute to set whether the current Model needs to be verified:

Add this attribute to the model. When you Post data, the MVC Framework will no longer automatically verify whether the current input is valid.

 

You will see that the Decription here has an AllowHtml attribute, which is added in ASP. net mvc 3 and has this attribute field. You can directly input HTML from the client .... But this is very dangerous, so do not use it easily. For example, you can see this cool:

On the Edit page, enter javascript and click Save:

The Script... will pop up .... However, this property can be used to demonstrate javascript running on the page .....

 

Nick

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.