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.