ZNS
ZNS

Reputation: 850

Asp.Net MVC - Tracking an id in route

I'd like to track an id in the route if it's possible? This is pretty much what I want:

/groups/{id} - Show group
/groups/{id}/forum/topic/{id} - Show forum topics for the group
/groups/{id]/calendar/ - Show the calendar for the group

As you see I want to keep track of what group the user is in by the url, instead of let's say a cookie.

Thanks!

Upvotes: 0

Views: 404

Answers (2)

James Lawruk
James Lawruk

Reputation: 31337

This would work as long as all the possible Urls are generated with the Id. For these routes, you can access the {id} using the following:

int id = int.Parse(RouteData.Values["id"].ToString());

It may be cumbersome to ensure every link on each page contained the Id. No static Urls would be allowed. For example, if the user clicked to the homepage or to /about, the Id would be lost.

Upvotes: 0

Matthew Abbott
Matthew Abbott

Reputation: 61589

Have you defined {id} in your routes, e.g.:

routes.MapRoute(
    "", 
    "groups/{id}/forum/topic/{topicId}",
    new { controller = "Forum", action = "Topic" });

And into your controller:

public class ForumController : Controller
{
    public ActionResult Topic(int id, int topicId)
    {

    }
}

It's not really clear how you want to track it....

Upvotes: 1

Related Questions