Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

asp.net mvc 4 - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...