0

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 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
I don't understand. Can you post an example?
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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.