MVC several problem solving methods that cannot find resources

Source: Internet
Author: User

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)]

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.