0

I'm learning MVC and faced this challenge which I can't figure out how. When I was at beginning I'm happy with such URL:

http://www.domain.com/stories/1

where 1 is the ID of one story in the database.

Then I decided to put chapters for each story, to read each chapter I used such URL:

http://www.domain.com/chapters/1

where 1 is the ID of chapter in the database.

Now, I want readers to see what is the ID of the story they are reading, here's my ideal URL:

http://www.domain.com/stories/1/chapters/2

But how can I achieve this?

Thanks in advance.

1
  • You can use the suggested methods (mapping routes) or perhaps Areas (which introduce additional mapped routes anyway) which force an organization of the code in the project. Commented Dec 14, 2011 at 5:50

2 Answers 2

2

What you are looking for is MapRoute.

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

2 Comments

I'm afraid I cannot learn without example. Can you please give me an example of MapRoute? I think it's more appropriate to use that instead of putting it in the QueryString.
Thank you so much, I thought the sample was wrong but after checking the names of directories, it worked! Thanks a lot!
1

You can achieve it using two different methods.

Method 1. (tested)

If you want in the stories cotroller, you have to creating a route map like this

   routes.MapRoute(
            "story-with-chapter", // Route name
            "stories/{storyid}/chapters/{chapterId}", // URL with parameters
            new { controller = "stories", action = "chapters"} // Parameter defaults
        );

and your action should look like this

public ActionResult Chapters(int storyid,int chapterId)
{


     return View();
}

Comments

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.