In the example project of pro mvc3 framework, the URL format in the navigation bar is: http: // xxx /? Category = Watersport
In booksCodeExample:
Code example in NAV Controller
Public class navcontroller: controller {private iproductrepository repository; Public navcontroller (iproductrepository repo) {repository = repo;} public partialviewresult menu (string Category = NULL) {viewbag. selectedcategory = category; ienumerable <string> categories = repository. products. select (x => X. category ). distinct (). orderby (x => X); Return partialview (categories );}}
Front-end page: page in menu. cshtml
@ Model ienumerable < String > @ {Layout =Null ;} @ Html. actionlink ( " Home " , " List " , " Product " ) @ Foreach ( VaR Link In Model) {@ html. routelink (link, New {controller = "product", Action = "list", Category = link, page = 1 }, New {@ Class = link = viewbag. selectedcategory? " Selected " : Null })}
The link generated by the sample code is shown in the previous article.
However, the Code in the subsequent books shows a link address similar to: http: // xxx/Watersport. If you follow the method in the book, you will not be able to get the address of this style.
As a result, I spent a lot of time finding out how to apply the style address:
1. First, in routercollection, name the router address style to be obtained,
1 Routes. ignoreroute ( " {Resource}. axd/{* pathinfo} " ); 2 3 Routes. maproute ( Null , "" , New {Controller = " Product " , Action = " List " , Categoray = ( String )Null , Page = 1 }); 4 5 Routes. maproute ( Null , " Page {page} " , New {Controller = " Product " , Action = " List " , Category = ( String ) Null } 6 , New {Page = @" D \ + " }); // Constraints: page must be numerical 7 8 Routes. maproute ( " Test " , " {Category} " , New {Controller = " Product " , Action = " List " , Page = 1 }); 9 10 Routes. maproute ( Null , " {Category}/Page {page} " , New {Controller = " Product " , Action = " List " , Category = ( String ) Null }, New {Page = @" \ D + " }); // Constraints: page must be numerical 11 // Routes. maproute (null, "{category}/Page {page}", new {controller = "product", Action = "list "}, new {page = @ "\ D + "}); 12 13 // Routes. maproute ( 14 // "Default ", // Route name 15 // "{Controller}/{action}/{ID }", // URL with Parameters 16 // New {controller = "product", Action = "list", id = urlparameter. optional} // Parameter defaults 17 // ); 18 Routes. maproute ( Null ," {Controller}/{action} " );
Name it "test"
2. modify the code of menu. cshtml:
1 @ Model ienumerable < String > 2 @{ 3 Layout = Null ; 4 } 5 6 @ Html. actionlink (" Home " , " List " , " Product " ) 7 @ Foreach ( VaR Link In Model) 8 { 9 @ Html. routelink (link, 10 // New {controller = "product", Action = "list", Category = link, page = 1 }, 11 " Test " , New {Category = Link }, 12 New {@ Class = link = viewbag. selectedcategory? " Selected " : Null }) 13 14 }
HTML is involved here. for routerlink () method, the first parameter is the text of the link, the second link is the name of the specified route table, and the third link is the route parameter, the fourth parameter is the CSS style of the value assignment link.
After modification, re-compile and run it to get the desired style.