Creating a route to support a reporting engine

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

How to do it...

  1. Create a new MVC application.
  2. Open up the global.asax file.
  3. Now we can add a couple of new routes after the 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" });
    
  4. With our routing in place, we now need to stand up some actions to handle the requests. Open up the 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");
    }
    
  5. Now you can add the new report action to support the Report view that our other actions will be returning.

    Controllers/HomeController.cs:

    public ActionResult Report()
    {
    return View();
    }
    
  6. With our actions set up to handle the routes that we created, we can add some super simple logic to pretend that we are actually building a report with the specified data. We will do this by creating a helper method that will build a report for us. This method will take in all the parameters that are passed into the reporting routes we created.

    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;
    }
    
  7. With our helper function in place, we can now plug it into our 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");
    }
    
  8. All that is left is to update our Report view to show the results of our generated report.

    Views/Home/Report.aspx:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Report</h2>
    <%: ViewData["report"] %>
    </asp:Content>
    
    
  9. Now you can build the application. Navigate to: http://localhost:{yourPort}/Report/salesReport/from/2010/1/1/to/2010/5/1. You should see the report view with the appropriate output.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset