In this recipe, we will take a look at how to create a route to support a reporting engine. In this case, we want to be able to capture a report name as well as the start and ending date of the report. We will attempt to support a route that looks something like this:
SalesReport/From/2010/01/01/To/2010/05/01
global.asax
file. IgnoreRoute
to support the url
format we discussed: SalesReport/From/2010/01/01/To/2010/05/01. Notice that each of these routes is going to be mapped to two different actions, named ReportPartial
and ReportComplete
.Global.asax:
routes.MapRoute("ReportPartial", "Report/{reportName}/from/{fromYear}/{fromMonth}/{fromDay}", new {controller = "Home", action = "ReportPartial"}); routes.MapRoute("ReportComplete", "Report/{reportName}/from/{fromYear}/{fromMonth}/{fromDay}/to/ {toYear}/{toMonth}/{toDay}", new { controller = "Home", action = "ReportComplete" });
HomeController
file. Then add two new actions—one called ReportPartial
and the other called ReportComplete
. Both of these actions will return a view that does not map to their name. Specifically, we will set each of these actions to return a Report view.Controllers/HomeController.cs:
public ActionResult ReportPartial(string reportName, int fromYear, int fromMonth, int fromDay) { return View("Report"); } public ActionResult ReportComplete(string reportName, int fromYear, int fromMonth, int fromDay, int toYear, int toMonth, int toDay) { return View("Report"); }
Controllers/HomeController.cs:
public ActionResult Report() { return View(); }
Controllers/HomeController.cs:
public string GenerateReport(string reportName, int fromYear, int fromMonth, int fromDay, int toYear, int toMonth, int toDay) { string result = ""; if (toYear == 0) result = "The " + reportName + " shows great financial figures from " + fromMonth + "/" + fromDay + "/" + fromYear + " to present!"; else result = "The " + reportName + " shows great financial figures from " + fromMonth + "/" + fromDay + "/" + fromYear + " to " + toMonth + "/" + toDay + "/" + toYear + "!"; return result; }
ReportPartial
and ReportComplete
actions. We will put the result of our GenerateReport
method into ViewData
to be passed down to our Report
action.Controllers/HomeController.cs:
public ActionResult ReportPartial(string reportName, int fromYear, int fromMonth, int fromDay) { ViewData["report"] = GenerateReport(reportName, fromYear, fromMonth, fromDay, 0, 0, 0); return View("Report"); } public ActionResult ReportComplete(string reportName, int fromYear, int fromMonth, int fromDay, int toYear, int toMonth, int toDay) { ViewData["report"] = GenerateReport(reportName, fromYear, fromMonth, fromDay, toYear, toMonth, toDay); return View("Report"); }
Views/Home/Report.aspx:
http://localhost:{yourPort}/Report/salesReport/from/2010/1/1/to/2010/5/1
. You should see the report view with the appropriate output.In this recipe, we created two routes—one to support the specification of just the start of the report and the other route to capture the start and end date of the report. To make sure that we don't end up writing the same code over and over again, we moved the report generation into a helper method (in reality this would probably go into your business layer). We then passed the output of the helper method down to the report view, where we actually show the results of the report.