ASP. net mvc sets route so that the controller name is not displayed in the URL

Source: Internet
Author: User

Introduction

Create an ASP. in the example automatically generated when a net MVC 1.0 project is created, the default route is {controller}/{action}/{ID}. Here the ID is a parameter and can be omitted, however, the Controller and action must be available, so that the controller and action must be specified in the URL. For example, if you want to access the login page in that example, you must write it as http: // localhost/account/login. if our account system is developed independently, we have already set a virtual directory named account. If we want to specify in the URL that the controller is an account, the URL will become similar to http: // localhost/account/login. The following is to omit the controller in the URL. In fact, it is similar to putting the content in the Account view under the root directory, but we cannot directly do this, But modify the route rule. This method is suitable for applications that are already in a virtual directory and have only one controller.ProgramIf the application logic is complex and the number of controllers is large, it is not suitable.

Implementation

The final solution is to replace the original http: // localhost/{action} URL with a URL similar to http: // localhost/account/{action}. The original http: // localhost/home/{action} is not affected. First, replace the original routes. maproute block

 

1 Routes. maproute (
2 " Test " ,
3 " {Action} " ,
4 New   {Controller= "Account", Action= "Login"}
5 );

 

Where test is the rule name and {action} is the rule matching the URL. New {controller = "Account", Action = "login"} indicates that the default controller is the account, the default action is login. Because the contrler name is not provided in the matched URL, the final contrler used by the request whose URL matches this rule is an account. In this way, the contrler name will no longer appear in the URL. However, after this is done, the content in/home/{action} cannot be accessed. If you want to make the content in home accessible, you need to add another route rule to match/home/{action }.CodeAdd

 

1 Routes. maproute (
2 " Default " ,
3 " {Controller}/{action} " ,
4 New   {Controller= "Home", Action= "Index"}
5 );

 

If you change {controller}/{action} to home/{action}, but this only matches the home controller, there is no difference between the current example and the above Code functions, if other controllers are added, the newly added controller cannot be matched. In addition, the order of the Code for adding the route rule in the above two sections cannot be reversed, because there will be an order when matching the URL, and once the match is successful, it will not continue to match. If you put the following section above, entering http: // localhost/login in the address bar will match controller = login, Action = index, which is not what we want.

After the above modification, it seems that the account folder is under the root directory, and the home folder is under the Account folder.

This article applies to ASP. net mvc 1.0

 

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.