Several MVC methods cannot find resources, and several mvc methods cannot find resources
In MVC, the Action in the controller corresponds to the. cshtml file name in the View.
When the error occurs, the following situations occur:
1. view not found Error
Request URL: http: // localhost: 13850/Customer/Create
The controller has the corresponding Action:
View does not have the corresponding View
Error message:
Solution: add the corresponding View to the View.
2. Unable to find the resource:
Request URL: http: // localhost: 13850/Customer/Index
There is no corresponding Action in the controller (sometimes the Action name is modified after a period of development, but the View name is not modified accordingly, but the requested URL is still caused by the original)
View.
Error message:
The solution is to specify a fixed "page" and display it when the resource cannot be found. The details are as follows:
The controller in Asp. Net MVC is an abstract class integrated from the Controller. In this class, a HandleUnknownAction method is defined, which is processed when HTTP 404 occurs.
Then, we can rewrite this method in our controller.
In this way, this method can be executed every time the corresponding Action cannot be found. Call the corresponding View.
Microsoft provides the following methods:
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, this also has a hidden danger. In the case of the following [3], only one HTTP is declared. the Post method does not have the corresponding Http. the Get method can also load the page.
How can this problem be solved? The answer is: directly Response. Redirect ("/", true) and return to the home page.
3. Unable to find the resource:
Request URL: http: // localhost: 13850/Customer/Details
The corresponding Action in the controller, defined as HTTP Post, and there is no corresponding Http Get Action
View has a corresponding View
Error message:
Solution: add an Action with the same name, defined as http get: [AcceptVerbs (HttpVerbs. Get)]
4: This is why I have not found it for half a day. Finally, I found it at http://www.cnblogs.com/xyang/archive/2011/11/24/2262003.html.
For more information, see http://www.cnblogs.com/xyang/archive/2011/11/24/2262003.html #.