Asp.net mvc routes various activities on one controller

I have a controller with a name Raportarethat has two actions: ReportAand ReportB. Both return an excel file based on the provided parameters.

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 to mysite.com/Raportare/ReportB/5/2012, it returns a ReportA file. It works great if I go to mysite.com/Raportare/ReportB?month=5&year=2012. I'm probably doing something wrong in the routing rules, but I can't figure it out.

+3
source share
2 answers

You do not need to add a route for each action - they work as templates, and the third parameter is only the default values.

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

Global.asax.cs, .

mysite.com/Raportare/ReportB/5/2012 ReportB, URL-.

mysite.com/Raportare ReportA, .

+3

. , , , .

0

All Articles