MVC5& SSRS ReportViewer - 如何实施?

时间:2015-06-08 12:57:42

标签: c# asp.net asp.net-mvc reporting-services

我花了好几个小时试图解决这个问题,到目前为止我可以找到基于MVC1,MVC2和MVC3的解决方案,但是没有关于MVC5和使用SSRS和ReportViewer的解决方案。坦率地说,我不了解WebForms,因为我加入了编程世界之后,MVC在我工作的商店中是一件大事。足够多的背景故事,我会接受它。

我有:

  • 编写存储过程
  • 使用VS 2013中的商业智能工具设计我的报告
  • 查看设计师的报告并知道它很好
  • 尝试将ReportViewer集成到我的应用程序中但没有成功
  • 确定它似乎需要重新执行我的查询调用(尽管它在报表设计中),并使用一组测试日期编写代码来执行此操作
  • 确定的WebForms以某种方式参与让我的报告显示在我的应用程序中。

所以我已经达到了认为的程度我只需要aspx文件来完成我需要做的事情。如果有人能够看到这个并帮助我,你就能治好我几个小时的压力。

首先,我的RouteConfig.cs文件的片段:

routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

我的报告控制器代码:

     [HttpPost]
        public ActionResult GetSysWideQuizReport([Bind(Include = "Topic, Date1, Date2")] QuizReporting quizParams)
        {
 ReportViewer ReportViewer1 = new ReportViewer();
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportViewer1.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\System Quiz Report.rdl";
            ReportDataSource source = new ReportDataSource("DataSet1", QuizData(quizParams));
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(source);

            return View(ReportViewer1);
        }



private DataTable QuizData(QuizReporting quizParams)
        {

            DataSet ds = new DataSet("DataSet1");

                using(SqlConnection connection = new SqlConnection())
                { 

                connection.ConnectionString = GetConnectionString();

                SqlCommand cmd = new SqlCommand("SystemQuizReport", connection);
                //cmd.CommandText = "EXEC SchoolQuizReport @TopicID, @Date1, @Date2";
                cmd.Parameters.AddWithValue("@TopicID", quizParams.Topic.TopicID);
                cmd.Parameters.AddWithValue("@Date1", "2015/04/13");
                cmd.Parameters.AddWithValue("@Date2", "2015/04/16");
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);
                return ds.Tables[0];
                }

        }

        static private string GetConnectionString()
    {
        return "Data Source=(localdb)\\v11.0; Initial Catalog=UCAPDB-20140822124213; Integrated Security=True;";
    }

我的“查看”(真的是aspx)代码:

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="600">
    </rsweb:ReportViewer>
    </form>
</body>
</html>

我当前的错误是:

  

'〜/ Views / Reporting / GetSysWideQuizReport.aspx'中的视图必须从ViewPage,ViewPage,ViewUserControl或ViewUserControl派生。

更新

我已经尝试了下面的建议解决方案,显然很难做到这一点。所以我继续前进,然后将RDL转换为RDLC并使用设计向导将调用插入到ASPX页面的ReportViewer中。现在我收到了大量奇怪的AJAX错误。

更新了GetSysWideQuizReport.aspx:

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Quiz Report</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="915px" Height ="1000px" Font-Names="Lato" Font-Size="10pt" ShowBackButton="False" ShowRefreshButton="False" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt">
        <LocalReport ReportPath="ReportViews\System Quiz Report.rdlc">
            <DataSources>
                <rsweb:ReportDataSource DataSourceId="LocalDb" Name="DataSet1" />
            </DataSources>
        </LocalReport>
    </rsweb:ReportViewer>
        <asp:SqlDataSource ID="LocalDb" runat="server" ConnectionString="<%$ ConnectionStrings:LocalDb %>" SelectCommand="SystemQuizReport" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:FormParameter FormField="TopicID" Name="TopicID" Type="Int32" />
                <asp:FormParameter DbType="DateTime2" FormField="Date1" Name="Date1" />
                <asp:FormParameter DbType="DateTime2" FormField="Date2" Name="Date2" />
            </SelectParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>

用户表单直接发布到此aspx页面。现在我的错误:

Uncaught SyntaxError: Unexpected token <    ScriptResource.axd:1 
Uncaught SyntaxError: Unexpected token <    GetSysWideQuizReport.aspx:40
Uncaught Error: ASP.NET Ajax client-side framework failed to load.   ScriptResource.axd:1 
Uncaught SyntaxError: Unexpected token < Reserved.ReportViewerWebControl.axd:1 
Uncaught SyntaxError: Unexpected token < ScriptResource.axd:1 
Uncaught SyntaxError: Unexpected token <    GetSysWideQuizReport.aspx:49
Uncaught ReferenceError: Sys is not defined GetSysWideQuizReport.aspx:114 
Uncaught ReferenceError: Sys is not defined GetSysWideQuizReport.aspx:55 
Uncaught ReferenceError: $get is not defined

1 个答案:

答案 0 :(得分:4)

你必须使用ASPX页面,只需在你的MVC应用程序的根目录下添加一个新文件夹(例如:WebForms)并在其中添加一个新的ASPX webform。

在aspx页面中,您可以添加reportviewer

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

在aspx页面后面的代码中

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

有了这个,您应该可以导航到http://localhost:port/webForms/yourpage.aspx?reportfolder=[reportpath]&param1_RAP=1&param2_RAP=ogjirewog

您需要在后面的代码中提供以下参数 - ReportserverUser&amp; ReportserverPass:可以登录到reportserver界面并访问报告的凭据。 - Reportserver Url:这是reportserver Asxm webservice的url - reportpath:reportingserver中报告的路径(仅限名称,末尾没有扩展名)