Several MVC methods cannot find resources, and several mvc methods cannot find resources

Source: Internet
Author: User

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 #.

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.