c# - Reformating the URL's generated by the routing system -


hi have been trying solve problem quite while related routing , links no luck.

i have application stores number of books. each book has links displays it's details. in order details using id each book in link. code link:

@html.actionlink(book.name, "details", new { bookid = book.id , book = book.bookurl }) 

when user clicks 1 of links controller being called , not allowed modify it:

 public actionresult details (int bookid){..} 

and how link displayed:

http://localhost:51208/home/details/c-sharp-5.0-in-a-nutshell-the-definitive-reference/bookid-2 

what someway stop displaying controler action , bookid.so link this:

http://localhost:51208/c-sharp-5.0-in-a-nutshell-the-definitive-reference 

from can tell far there no way using standard functionality of routing figured have extend it's functionality somehow.

i have been reading in pro asp.net mvc 4 , have read have create class , inherit routebase.

this result in having implement 2 methods 1 routedata getroutedata(httpcontextbase httpcontext) , other virtualpathdata getvirtualpath(requestcontext requestcontext, routevaluedictionary values)

so far understand getvirtualpath responsible generating links figure have create implementation functionality desire.

if case can give me idea on how should implement method?

if not route take how can achieve want?

you implementing own version of routebase. need implement both getroutedata , getvirtualpath.

your implementation of getroutedata need check database of books see if url contains name of book in database, , return correct route data e.g.

public class bookroute : routebase {     public override routedata getroutedata(httpcontextbase httpcontext)     {         var url = httpcontext.request.apprelativecurrentexecutionfilepath;         url = url.replace(@"~/", "");          var book = getbookwiththenamethatmatchestheurl(url);          if (book != null)         {             var rd = new routedata(this, new mvcroutehandler());             rd.values.add("controller", "home");             rd.values.add("action", "details");             rd.values.add("bookid", book.id);             return rd;         }          return null;     } 

and implementation of getvirtualpath need opposite - find name of book given id, , return appropriate url.

e.g.

public override virtualpathdata getvirtualpath(requestcontext requestcontext,                                                 routevaluedictionary values) {     if ((string)values["action"] == "details" &&          (string)values["controller"] == "home")     {         var bookid = (int)values["bookid"];         var bookname = lookupthebooknameforthebookwiththisid(bookid);         return new virtualpathdata(this, bookname);         }      return null; } 

lastly, add routes.add(new bookroute()); registering routes enable new route checked.

given method getbookwiththenamethatmatchestheurl() hit database every page, you'd want caching look-ups.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -