3

I have a controller named Raportare that has two actions: ReportA and ReportB. Both return an excel file based on the parameters supplied.

public ActionResult ReportA(int? month, int? year)
{
...
}
public ActionResult ReportB(int? month, int? year)
{
...
}

My global.asax has the following routing rules for this :

routes.MapRoute(
                "ReportA",
                "{Raportare}/{ReportA}/{month}/{year}",
                new { controller = "Raportare", action = "ReportA", month = UrlParameter.Optional, year = UrlParameter.Optional});

 routes.MapRoute(
                "ReportB",
                "{Raportare}/{ReportB}/{month}/{year}",
                new { controller = "Raportare", action = "ReportB", month = UrlParameter.Optional, year = UrlParameter.Optional }); 

However when I go mysite.com/Raportare/ReportB/5/2012 it's returning the ReportA file. It works fine if I go to mysite.com/Raportare/ReportB?month=5&year=2012. Probably I'm doing something wrong in the routing rules but I can't figure it out.

2 Answers 2

3

You don't need to add a route for every action - they work like templates and the 3rd parameter is just default values.

routes.MapRoute(
  "reports",
  "Raportare/{action}/{month}/{year}",
  new {
    controller = "Raportare",
    action = "ReportA",
    month = UrlParameter.Optional,
    year = UrlParameter.Optional
  }
);

Put this before your default rule in Global.asax.cs, otherwise the default might match first.

Then mysite.com/Raportare/ReportB/5/2012 will invoke the ReportB action because it has been specified in the url.

mysite.com/Raportare will invoke ReportA, because it is the default action.

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

Comments

0

The routes you've created are basically the same. That's why the first one which matches request string succeeds and first action gets invoked.

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.