Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I render a remote ReportViewer aspx page in MVC4?

I'm working on an MVC4 application that needs to render a remote report from SSRS using the ReportViewer. With help from this forum, I've managed to get the page to render under MVC, but callbacks won't work (loads the initial page). Exporting the report works fine (and gives all pages). When I inspect the page, I noticed the following error after changing pages:

Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

I found this article on combining MVC and Web Forms, but it appears outdated since there are no more Master layout pages. This is related to but not a duplicate of How can I use a reportviewer control in an asp.net mvc 3 razor view? since that article is only for local reports. I've tried changing AsyncRendering to true and false. When true, it doesn't load at all. Any suggestions would be greatly appreciated.

Update: AsyncRendering behavior appears to have changed between previous versions of Visual Studio.

like image 530
Elsimer Avatar asked Mar 14 '13 23:03

Elsimer


People also ask

How do I display an SSRS report?

Use Page Viewer web part. Select Web Part Page, insert Page Viewer web part, and put the url of SSRS into the web part.


1 Answers

In the end, I ended up having to ditch both my original answer and the callbacks criteria due to the unacceptable security risks. In my case, I wrote controller code rendering the report as HTML to a byte array and from there to a FileContentResult that MVC was kind enough to render as a static HTML page. Exporting as PDF, Excel, or any other options will eventually be implemented in a similar method by changing the Render parameter from HTML4.0 to whatever is appropriate (PDF,XLS) and the MIME type. This approach works with SQL Server 2008R2 and beyond. I haven't tried it with previous versions of SQL Server.

[OutputCache(Duration = 120, VaryByParam = "id")]
public ActionResult ExportHTML(int id)
{
    // we need to add code to check the user's access to the preliminary report.
    // Also need to consolidate code between ExportHTML and ExportPDF.
    var userid = <userid>;
    var password = <password>;
    var domain = <domain>;
    IReportServerCredentials irsc = new myApp.Models.CustomReportCredentials(userid,
        password, domain);
    var parametersCollection = new List<ReportParameter>();
    parametersCollection.Add(new ReportParameter("Snapshot", id.ToString(), false));
    ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
    rv.ProcessingMode = ProcessingMode.Remote;
    rv.ServerReport.ReportServerCredentials = irsc;
    rv.ServerReport.ReportPath = <reportpath>;
    rv.ServerReport.ReportServerUrl = new Uri("http://localhost/ReportServer");
    rv.ServerReport.SetParameters(parametersCollection);

    rv.ServerReport.Refresh();
    byte[] streamBytes = null;
    string mimeType = "";
    string encoding = "";
    string filenameExtension = "";
    string[] streamids = null;
    Warning[] warnings = null;

    streamBytes = rv.ServerReport.Render("HTML4.0", null, out mimeType, out encoding,
                                         out filenameExtension, out stream ids,
                                         out warnings);
    var HTMLReport = File(streamBytes, "text/html");
    return HTMLReport;
}
like image 138
Elsimer Avatar answered Oct 21 '22 16:10

Elsimer