The default url pattern is {controller}/{action}/{id}
but I want it of the type {db-field-which-is-retrieved-using-id}/{controller}/{action}/{id}
I want space where I can retrieve the db-field
1 Answer
You can use whatever pattern you like.
_routes.MapRoute("myRoute","view/{db-field}/{controller}/{action}/{id}",
new { controller = "home", action = "index"}
);
What's important is to have a path segment('view' in this example) and to declare the route before others so that it can use this route and not the default one. I'm assuming the db-field is something like an article title and you want it for SEO purposes.
4 Comments
Swapnil Gondkar
I want to generate url using the database so where exactly can I retrieve the data from that id ? my links do not have that host, I want to generate it when someone sees that page.
MikeSW
I don't understand. Can you post an example?
Swapnil Gondkar
ok see if I have {teacher-name}/{controller}/{action}/{student-id} now I want to extract the teachers name automatically from the database every time i get the student id.I don't want to add that extra parameter of teacher-name in the url action link on the viewside. each time routing engine encounters a parameter of student-id it should run a function which returns him the teacher-name.Thanks Mike
MikeSW
If I understand correctly your problem has nothing to do with routing. You get the teacher name from the database in the controller using the student-id parameter, pass it to a view via a viewmodel and that's it. THe teacher name from url should be only for SEO or to make the teacher happy. The routing engine just identifies the controller, action and varioous action parameters. In this case the parameter should be student-id