Transferred from: http://www.cnblogs.com/xyang/archive/2011/11/24/2262003.html
In MVC, the action in the controller and the name of the. cshtml file in the view have a corresponding relationship.
When not corresponding, there are several situations that occur:
One, unable to find the error of the view
Request Url:http://localhost:13850/customer/create
There is a corresponding action in the controller:
There are no views in the view
Error message:
Workaround: Add the corresponding view in the view
Second, the resources can not be found:
Url:http://localhost:13850/customer/index of the request
There is no corresponding action in the controller (sometimes it is possible to modify the name of the action after a period of development, but the name of the view is not modified, but the requested URL is still the original result)
View with corresponding views
Error message:
The workaround is to specify a fixed "page" that appears when a resource is not found. Specific as follows:
Controller in ASP. NET MVC is an abstract class that is integrated with the self controller, in which a handleunknownaction method is defined, and the left and right is handled when HTTP 404 occurs.
Well, we can rewrite this method in our own controller.
This way, you can do this every time you can't find a corresponding action. To invoke the corresponding view.
The way Microsoft offers is:
Protected override void Handleunknownaction (string actionname)
{
try {
This. View (ActionName). Executeresult (This. ControllerContext);
} catch (InvalidOperationException Ieox)
{
viewdata["error"] = "Unknown Action: \" "+server.htmlencode (actionname) + " \ ";
viewdata["exmessage"] = Ieox. Message;
This. View ("Error"). Executeresult (This. ControllerContext);
}
}
That is, the output error page.
Of course, there is a hidden danger, if the following "three" situation, that is, only declare a http.post method, there is no corresponding http.get of the same name method, can also be used in this way, the page loaded out.
How to solve it? The answer is: direct Response.Redirect ("/", true), back to the homepage.
Third, the resources can not be found:
Request Url:http://localhost:13850/customer/details
The corresponding action in the controller is defined as an HTTP Post, and there is no action for the corresponding HTTP GET
View with corresponding views
Error message:
Workaround: Add an action of the same name, defined as HTTP Get:[acceptverbs (Httpverbs.get)]