开发者

How can we Route something like {calendar}/{controller}/{action}/{id}

开发者 https://www.devze.com 2023-01-18 21:48 出处:网络
As a beginner in MVC I\'m trying to figure out what\'s the best way to accomplish a route for my needs, and I\'m getting no luck at all, so I\'m kindly ask for any help

As a beginner in MVC I'm trying to figure out what's the best way to accomplish a route for my needs, and I'm getting no luck at all, so I'm kindly ask for any help

My webApp is driven by Calendars, and per each Calendar there are a bunch of actions

  • Subscribe
  • Edit Calendar
  • View Winners
  • Daily Challenge
  • etc...

and I would like to avoid passing something like

mydomain.com/calendar/2

my idea would be to hide the ID so no one can ha开发者_如何学运维ve logical access to other calendars, for example

mydomain.com/q2tsT

where q2tsT could be a 10 char random generated string and then I would like to have routes such as:

mydomain.com/q2tsT/subscribe
mydomain.com/q2tsT/daily-challenge
mydomain.com/q2tsT/winners
mydomain.com/q2tsT/prizes

How would I set up my Route to perform like this?

something like:

routes.MapRoute(
    "CalendarRoute",
    "{calendar}/{controller}/{action}/{id}",
    new { 
        calendar = "Empty", 
        controller = "Frontend", 
        action = "Index", 
        id = UrlParameter.Optional }
);

but what will I do with the calendar? where do I pick it up so I can convert to an ID so querying the DB would be faster

I'm kinda lost here :(


Firstly, even by 'hiding' the calendar ID behind a string there is still nothing to stop a user from trying to guess an appropriate ID. Whatever solution you use will need to have security built in to the actions to ensure the appropriate data is only shown to authorized users.

To get the calendar - just pass the string value into your action method and decode it there.

routes.MapRoute(
    "CalendarRoute",
    "{calendar}/{controller}/{action}/{id}",
    new { 
        calendar = "Empty", 
        controller = "Frontend", 
        action = "Index", 
        id = UrlParameter.Optional }
);

public ActionResult Index(string calendar, int? id) {
   // decode the calendar into something useful
...    
}
0

精彩评论

暂无评论...
验证码 换一张
取 消