本文介绍了如何在 MVC4 中呈现远程 ReportViewer aspx 页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 MVC4 应用程序,该应用程序需要使用 ReportViewer 从 SSRS 呈现远程报告.在这个论坛的帮助下,我设法让页面在 MVC 下呈现,但回调不起作用(加载初始页面).导出报告工作正常(并提供所有页面).当我检查页面时,在更改页面后发现以下错误:

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:

未捕获的 Sys.WebForms.PageRequestManagerParserErrorException:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息.

我发现 这篇关于结合 MVC 和 Web 窗体的文章,但它看起来已经过时,因为没有更多的主布局页面.这与 如何在 asp.net mvc 3 razor 视图中使用报表查看器控件? 因为该文章仅适用于本地报表.我试过将 AsyncRendering 更改为 true 和 false.当为真时,它根本不加载.任何建议将不胜感激.

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.

更新:AsyncRendering 行为 似乎在以前版本的 Visual Studio 之间发生了变化.

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

推荐答案

最后,由于不可接受的安全风险,我不得不放弃我的原始答案和回调标准.就我而言,我编写了控制器代码,将报告作为 HTML 呈现到一个字节数组,然后从那里到一个 FileContentResult,MVC 足以呈现为静态 HTML 页面.导出为 PDF、Excel 或任何其他选项最终将通过将渲染参数从 HTML4.0 更改为适当的(PDF、XLS)和 MIME 类型以类似的方法实现.此方法适用于 SQL Server 2008R2 及更高版本.我还没有用以前版本的 SQL Server 尝试过.

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

这篇关于如何在 MVC4 中呈现远程 ReportViewer aspx 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:55