ASP. net mvc Tip #22-directly return a view without creating a controller action

Source: Internet
Author: User

ASP. net mvc Tip #22-directly return a view without creating a controller action
ASP. net mvc Tip #22-return a view without creating a controller action

Original American: http://weblogs.asp.net/stephenwalther/archive/2008/07/21/asp-net-mvc-tip-22-return-a-view-without-creating-a-controller-action.aspx
Http://www.cnblogs.com/mike108mvp

Note: The translation level is limited. If there are any mistakes or mistakes in the translation, you are welcome to criticize and correct them. Thank you.

Note: ASP. net mvc qq chat group 1215279 welcome to friends interested in ASP. NET MVC

In this post, I will demonstrate how to directly return the view without using the Controller method. I will show you how to use the handleunknownaction method to automatically process each request to a controller.

I saw Phil haack using the following techniques in a demo. I think this is a good technique. I have to share it with you.

Do not write unless you have a good reasonCode. I found that I wrote a lot of controller actions, and they didn't do anything, but returned a view. For example, see customercontroller in Listing 1 below:

Listing 1-customercontroller. CS

The controller contains three actions and returns three different views. Each action contains a line of code. In fact, each action contains the same line of code. This line of code is not required. How can I modify it?

The Controller class contains a handleunknownaction () method, which is executed when you try to call a nonexistent action. The Controller in Listing 2 uses the handleunknownaction () method to present the view if a corresponding Controller method is not found,

Listing 2-homecontroller. CS

When you use the Controller in Listing 2, you can call any action, and the Controller will try to return a corresponding view. You do not need to explicitly write the action method code for each view.

Note that the controller contains a details () action. When you need to pass viewdata, You need to explicitly write the action method.

 

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.