In the mvc3 example project, how can I change the link in the navigation from a parameter with a question mark to a link without a suffix?

Source: Internet
Author: User
Tags actionlink

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.

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.