MVC5 & SSRS ReportViewer - How to Implement?

这一生的挚爱 提交于 2019-12-04 03:55:40

you have to use a ASPX page, just add a new folder (ex: WebForms) under the root of you MVC application and add a new ASPX webform in it.

in the aspx page you can add the reportviewer

<rsweb:ReportViewer ID="reportViewer" runat="server" Width="100%" Height="800" CssClass="reportViewer" ShowPrintButton="False"> </rsweb:ReportViewer>

in the code behind of the aspx page

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ReportFolder"]))
            {
                string reportpath = HttpUtility.HtmlDecode(Request.QueryString["ReportFolder"]);
                int aantalKeys = Request.Params.AllKeys.Length;

                List<ReportParameter> parameters = new List<ReportParameter>();
                for (int i = 1; i < aantalKeys; i++)
                {

                    string value = Request.Params[i];
                    string key = Request.Params.Keys[i];
                    if (key.Contains("_RAP"))
                    {
                        int index = key.IndexOf('_');
                        key = key.Substring(0, index);
                        ReportParameter parameter = new ReportParameter(key, HttpUtility.HtmlDecode(value));
                        parameters.Add(parameter);
                    }
                }
                this.RenderReport(reportpath, parameters);
            }

        }

    }

    private void RenderReport(string reportpath, List<ReportParameter> parameters = null)
    {
        string User = [ReportserverUser];
        string Pass = [ReportserverPass];
        string ReportServerUrl = [ResportserverUrl]];
        IReportServerCredentials irsc = new CustomReportCredentials(User, Pass, "");
        Uri uri = new Uri(ReportServerUrl);
        int lastSegment = uri.Segments.Length - 1;
        string page = uri.Segments[lastSegment];

        // EVENTS
        //reportViewer.Load += reportViewer_Load;
        //reportViewer.Unload += reportViewer_Unload;

        reportViewer.Visible = true;
        reportViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
        reportViewer.ServerReport.ReportServerCredentials = irsc;
        reportViewer.ServerReport.ReportServerUrl = new Uri(uri.AbsoluteUri.Replace(page, ""));
        reportViewer.ServerReport.ReportPath = reportpath;
        if (parameters != null && parameters.Count != 0)
        {
            reportViewer.ServerReport.SetParameters(parameters);

        }
        reportViewer.ServerReport.Refresh();
    }

    private Dictionary<string, object> GetCurrentParameters()
    {

        var parameterCollection = reportViewer.ServerReport.GetParameters();


        var param = new Dictionary<string, object>();
        foreach (var p in parameterCollection)
        {
            var name = p.Name;
            if (p.DataType == ParameterDataType.DateTime)
            {
                var d = Convert.ToDateTime(p.Values[0]);
                param[name] = d.ToString("dd-MM-yyyy");
            }
            else
            {
                var values = p.Values.ToList();
                param[name] = values;
            }

        }


        return param;
    }

with this in place you should be able to navigate to http://localhost:port/webForms/yourpage.aspx?reportfolder=[reportpath]&param1_RAP=1&param2_RAP=ogjirewog

you need to provide the following param in the code behind - ReportserverUser & ReportserverPass: credentials that can login onto the reportserver interface and access the report. - Reportserver Url: this is the url to the reportserver Asxm webservice - reportpath: the path to the report in reportingserver (only the name, no extension at the end)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!