This project has moved and is read-only. For the latest updates, please go here.

Specify report type in ReportResult()

Jun 22, 2011 at 4:34 PM

What is the chance that you can add a parameter to ReportResult that would allow the specification of the report type as an alternative to using the controller extension?

return new ReportResult(report);


return new
ReportResult(report, ReportType.Excel);
Jun 22, 2011 at 8:36 PM

I can add that in, but I don't know when the next release will be.

In the mean time you can take a look at the source and write your own ReportResult, it's only a few dozen lines.

Ideally I should rewrite the ReportResult with a protected virtual method like "GetWriter", with the default implementation using the current logic, and anyone inheriting from ReportResult could override with their own logic for locating the IReportWriter they want. But like I said, not sure when that will be :)

Jun 23, 2011 at 12:58 AM

The reason this would be useful is to allow the user to select the report format from a dropdown when specifying parameters and then have the appropriate format dislayed.

Jun 23, 2011 at 1:50 PM

I changed the ReportResult definition to include an optional reportFormat string to allow specification of the format in code. This works like a charm. Code below, with changes in red.

namespace Doddle.Reporting.Web
    public class ReportResult : ActionResult
        private readonly Report _report;
        private readonly string _reportFormat;

        public ReportResult(Report report, string reportFormat = null)
            _report = report;
            _reportFormat = reportFormat;

        public override void ExecuteResult(ControllerContext context)
            string defaultExtension = Config.Report.Writers[Config.Report.DefaultWriter].FileExtension;

            // attempt to get the report format from the extension on the URL (ex. "/action/controller.pdf" yields ".pdf")
            string extension = Path.GetExtension(context.HttpContext.Request.RawUrl);

            // if we don't have a extension, use the requested format
            if (string.IsNullOrEmpty(extension))
                extension = _reportFormat;

            // if we still don't have a extension, use the default report format defined in the .config file
                extension = defaultExtension;

            WriterElement e = Config.Report.Writers.GetWriterConfigurationForFileExtension(extension);
            if(e == null)
                throw new InvalidOperationException(string.Format("Unable to locate a report writer for the extension '{0}'. Did you add this fileExtension to the web.config for DoddleReport?", extension));

            var writer = e.LoadWriter();

            context.HttpContext.Response.ContentType = e.ContentType;
            writer.WriteReport(_report, context.HttpContext.Response.OutputStream);

Nov 28, 2011 at 6:22 PM

Since the project has been converted to mercurial I'm going to publish some updates. I will get this change in tonight

Dec 2, 2011 at 5:30 PM

Hi jjorczak,

I've updated the ReportResult in the latest source code. It will be included in the updated NuGet package going out this weekend.

Thanks for the suggestion!