如何在MVC4中呈现远程ReportViewer aspx页面?

时间:2013-03-14 23:29:32

标签: asp.net-mvc-4 reporting-services webforms reportviewer

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

  

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

我发现this article结合了MVC和Web表单,但它似乎已经过时,因为没有更多的主布局页面。这与How can I use a reportviewer control in an asp.net mvc 3 razor view?相关但不重复,因为该文章仅适用于本地报告。我已经尝试将AsyncRendering更改为true和false。如果为true,则根本不加载。任何建议都将不胜感激。

更新:以前版本的Visual Studio之间的AsyncRendering行为appears to have changed

3 个答案:

答案 0 :(得分:6)

最后,由于不可接受的安全风险,我最终不得不放弃原来的答案和回调标准。在我的例子中,我编写了控制器代码,将报表呈现为HTML到字节数组,然后从那里到FileContentResult,MVC足以将其呈现为静态HTML页面。通过将Render参数从HTML4.0更改为适当的(PDF,XLS)和MIME类型,最终将以类似的方式实现导出为PDF,Excel或任何其他选项。此方法适用于SQL Server 2008R2及更高版本。我没有尝试使用以前版本的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;
}

答案 1 :(得分:0)

我仍然希望得到更好的答案,但与此同时,我的解决方案似乎符合标准。它使用了Kendo Web窗口(所以我想你理论上可以使用jQuery编写自己的窗口)。我还没有修改它来传递参数,但它是一个开始。我还没有保护重定向操作,因此用户目前可以查看源,从jQuery加载中获取URL,转到该地址并从那里获取基础报告URL。我将把它标记为ChildActionOnly或其他一些方法来确保该操作仅适用于我的窗口。我还发现我可以将报告呈现给HTML4.0,将其放在FileResult中并以这种方式加载内容 - 但报告是静态HTML。

查看:

    @(Html.Kendo().Grid(Model)
    .Name("IndexGrid")
    .Columns(col => 
    {
        col.Bound(c => c.SchoolYear);
        col.Bound(c => c.SubmissionTypeDesc);
        col.Bound(c => c.EntityDesc);
        col.Bound(c => c.SubmissionDate);
        col.Bound(c => c.UserName);
        col.Bound(c => c.Certified);
        col.Command(c => 
            {
                c.Custom("Edit")
                    .Text("View")
                    .Action("Edit", "Draft");
                c.Custom("Preview")
                    .Click("windowOpen");
                c.Custom("Certify")
                    .Action("Certify", "Draft");
                c.Custom("Download")
                    .Action("DumpExcel", "Draft");
            }
            ).Title("<b>Actions</b>")
            .HtmlAttributes(new { style = "width:200px;" });
    })
    .DataSource(ds => ds.Server()
        .Model(model => model.Id(pk => pk.snapshot_id))
        )
    .Sortable(sort => sort.Enabled(true).SortMode(GridSortMode.MultipleColumn).AllowUnsort(true))
    .Reorderable(reorder => reorder.Columns(true))
    .Groupable(group => group.Enabled(true))
    )
</article>

@(Html.Kendo().Window()
      .Name("window") //The name of the window is mandatory. It specifies the "id" attribute of the widget.
      .Title("Preliminary Report") //set the title of the window
      .LoadContentFrom("Redir", "Reports") //define the Action and Controller name
      .Visible(false)
      .Iframe(true)
      .Resizable()
      .Width(750)
      .Height(500)
      .Scrollable(false)
      .Draggable()
          .Actions(a =>
          {
              a.Refresh();
              a.Minimize();
              a.Maximize();
              a.Close();
          })

)
<script>
    function windowOpen(e) {
        e.preventDefault();
        var window = $("#window").data("kendoWindow");
        window.open();
    }
</script>

ReportController代码段:

public ActionResult Redir()
{
    return RedirectPermanent("../ASPReports/ReportForm.aspx");
}

ReportForm.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="~/ASPReports/ReportForm.aspx.cs" Inherits="MyApp.Reports.ReportForm"%>

<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="reportForm" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <rsweb:ReportViewer ID="mainReportViewer" runat="server"  SizeToReportContent="true">
        </rsweb:ReportViewer>
    </div>
    </form>
</body>
</html>

ReportForm.aspx.cs(代码隐藏):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // credentials - could pull from config
        var userid = ""; 
        var password = "";
        var domain = "";

        IReportServerCredentials irsc = new CustomReportCredentials(userid, password, domain);
        mainReportViewer.ServerReport.ReportServerCredentials = irsc;

        //mainReportViewer.ServerReport.ReportServerUrl =
        //    new Uri(ConfigurationManager.AppSettings["ReportServerUrl"]);
        mainReportViewer.ServerReport.ReportServerUrl =
            new Uri("http://localhost/ReportServer");
        mainReportViewer.ServerReport.ReportPath = "Path";


        mainReportViewer.ProcessingMode = ProcessingMode.Remote;
        mainReportViewer.ShowParameterPrompts = false;
        mainReportViewer.ShowRefreshButton = false;
        mainReportViewer.ShowWaitControlCancelLink = false;
        mainReportViewer.ShowBackButton = false;
        mainReportViewer.ShowCredentialPrompts = false;
        var parametersCollection = new List<ReportParameter>();
        //parametersCollection.Add(new ReportParameter("Snapshot", "##", false));
        mainReportViewer.ServerReport.SetParameters(parametersCollection);
        mainReportViewer.ServerReport.Refresh();
    }
}

答案 2 :(得分:-1)

只需使用IFRAME即可。创建另一个网站或虚拟目录,使用Web窗体创建应用程序,然后在MVC应用程序上的IFRAME中显示他的报表查看器页面。您可以使用查询字符串设置报告参数。我有很多次使用这种方式将报表查看器放入不同的系统。