1

I am working on a ASP.Net MVC project.
I have a particular controller action that accepts a date value in the form yyyy/mm/dd. So the URL becomes

http://localhost/MyProject/PublicReview/GetReviews/2012/10/29.

where GetReviews is an action and 2012/10/29 the parameter. My RouteConfig is as follows:

     public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
    }

How should I change the routevalues ? What should be the order of MapRoute values?

1 Answer 1

1

I've not tested this, but I'm guessing this will work in your scenario:

 routes.MapRoute( 
           "Reviews", "PublicReview/GetReviews/{year}/{month}/{day}" 
            { controller = "PublicReview", action = "GetReviews" };

Note that this will need your GetReviews method to have three properties of "year", "month" and "day". You'll then have to parse them into a DateTime.

Taken from http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs which uses "-" for date separators.

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

2 Comments

Thanks, It didn't work initially. Then I added the controller name to the url parameter: "PublicReview/GetReviews/{year}/{month}/{day}" Now it is working. Thanks a lot.
Excellent, glad you got it working. I'll update my answer with that extra parameter for other people in the same boat.

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.