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