Skip to content

ASP.NET Reporting

Matt Hidinger edited this page Apr 10, 2016 · 2 revisions

DoddleReport makes integration with ASP.NET a breeze.

ASP.NET MVC

For MVC friends I have provided a custom Action Result called ReportResult that will serve up the report automatically.

The ReportResult will actually read the extension of the request on the controller and return the correct type of report.

Customizing the extensions

Check out the Configuring DoddleReport documentation to map the URL extension with a specific Report Writer.

IIS Express Support

If you're debugging use IIS Express it won't route custom extensions in URLs through the controller by default. You have to set the follow attribute to make it behave properly.

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

IMPORTANT ROUTING STEP

DoddleReport can automatically determine which IReportWriter to use based on the file extension of the route. To enable this, you must do the following:

using DoddleReport.Web;

// put this line at the _top_ of your route configuration
routes.MapReportingRoute();

Next simply create your report like you normally would and return it inside an instance of ReportResult. DoddleReport will handle the rest.

// HTTP GET /productreport.pdf - will serve a PDF report
public ReportResult ProductReport()
{
    // Get the data for the report (any IEnumerable will work)
    var query = ProductRepository.GetAll();
    var totalProducts = query.Count;
    var totalOrders = query.Sum(p => p.OrderCount);


    // Create the report and turn our query into a ReportSource
    var report = new Report(query.ToReportSource());


    // Customize the Text Fields
    report.TextFields.Title = "Products Report";
    report.RenderHints.BooleanCheckboxes = true;

    report.DataFields["Id"].Hidden = true;
    report.DataFields["Price"].DataFormatString = "{0:c}";
    report.DataFields["LastPurchase"].DataFormatString = "{0:d}";

    // Return the ReportResult
    // the type of report that is rendered will be determined by the extension in the URL (.pdf, .xls, .html, etc)
    return new ReportResult(report);
}

ASP.NET WebForms

The simplest way to add reporting to an ASP.NET web forms application is to add a "Generic Handler (.ashx)" to your project. If you are unfamiliar with .ashx files, they are basic HTTP handlers like .aspx, but they do not have the overhead of building a Page object and a control hierarchy. They are for serving any kind of content over the web, including HTML, binary data like PDF, images, etc. They are accessed via URL just like a regular .aspx, and can be passed query strings the same way.

WebReport will handle all serving of the report for you. It will determine what type of report to send based on the ?ReportType querystring.

Configuration

Check out the Configuring DoddleReport documentation to map the ReportType parameter to specific Report Writers.

When you create your ashx you can replace all the code inside as follows, you just need to implement a few basic abstract members.

public class ProductsReport : WebReport
{
    public override string FileName
    {
        get { return "ProductsReport"; }
    }

    public override IReportSource ReportSource
    {
        get
        {
            // LINQ query
            var query = from p in db.Products
                        select p;

            return query.ToReportSource();
        }
    }

    public override void CustomizeReport(Report report)
    {
        // Text Fields
        report.TextFields.Title = "Products Report";
        report.TextFields.Footer = "Copyright 2009 &copy; The Doddle Project";
        report.TextFields.Header = string.Format("Report Generated: {0}", DateTime.Now);

        // Data Fields
        report.DataFields["ProductID"].Hidden = true;
        report.DataFields["CategoryID"].Hidden = true;
        report.DataFields["SupplierID"].Hidden = true;
    }
}