MVC5和SSRS ReportViewer

我花了几个小时试图解决这个问题,到目前为止,我可以找到基于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) + @"ReportsSystem 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。

    UPDATE

    我已经尝试了下面的建议解决方案,显然在这方面很难做到。 所以我继续前进,并将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="ReportViewsSystem 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
    

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

    在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&ReportserverPass:可登录到reportserver界面并访问报告的凭证。 - Reportserver Url:这是reportserver的网址Asxm webservice - reportpath:报告服务器中报告的路径(只有名称,末尾没有扩展名)

    链接地址: http://www.djcxy.com/p/26829.html

    上一篇: MVC5 & SSRS ReportViewer

    下一篇: how to share different urls via uiactivityviewcontroller?