locked
SSRS Report Viewer in MVC4 RRS feed

  • Question

  • User1903393923 posted

    We are using SSRS report viewer to preview the reports on our website built on MVC4 razor engine. Does anybody know how to integrate the SSRS report viewer control on MVC4?

    Thursday, January 23, 2014 5:18 AM

Answers

  • User421908416 posted

    you need to create the view like ReportView.acs and then you need to create a report View Model, please take a look.

    View: ReportViewer.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ReportViewerControl.ascx.cs" Inherits="ReportViewerControl" %>
    <%@ Register TagPrefix="rsweb" Namespace="Microsoft.Reporting.WebForms" Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
    
    <form id="form1" runat="server">
    <div style="Height:720px;Width:800px">
        <asp:ScriptManager ID="scriptManager" runat="server" ScriptMode="Release" EnablePartialRendering="false" />
        <rsweb:ReportViewer Width="100%" Height="100%" ID="reportViewer" ShowPrintButton="true" KeepSessionAlive="true" runat="server" AsyncRendering="false" ProcessingMode="Remote">
             <ServerReport />
        </rsweb:ReportViewer>
    </div>
    </form>

    ReportViewer.ascx.cs

    namespace 
    {
        public partial class ReportViewerControl : System.Web.Mvc.ViewUserControl
        {
            protected void Page_Init(object sender, EventArgs e)
            {
                // Required for report events to be handled properly.
                //reportViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials();
                Context.Handler = Page;
                
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {                
                    ReportingServicesReportViewModel model = (ReportingServicesReportViewModel)Model;
                    reportViewer.ServerReport.ReportServerCredentials = model.ServerCredentials;
                    ReportParameter[] RptParameters = model.parameters;
                    
                    reportViewer.ServerReport.ReportPath = model.ReportPath;
                    reportViewer.ServerReport.ReportServerUrl = model.ReportServerURL;                
    
                    if(RptParameters.Count() > 0)
                    this.reportViewer.ServerReport.SetParameters(RptParameters);
                    this.reportViewer.ServerReport.Refresh();
                    
                }            
            }
        }   
    }

    ReportingServiceViewModel.cs

        public class ReportingServicesReportViewModel
        {
            #region Constructor
            public ReportingServicesReportViewModel(String reportPath,List<ReportParameter> Parameters)
            {
                ReportPath = reportPath;
                parameters = Parameters.ToArray();
            }
            public ReportingServicesReportViewModel()
            {           
            }
            #endregion Constructor
    
            #region Public Properties
            public ReportServerCredentials ServerCredentials { get { return new ReportServerCredentials(); } }
            public String ReportPath { get; set; }
            public Uri ReportServerURL { get { return new Uri(WebConfigurationManager.AppSettings["ReportServerUrl"]); } }
            public ReportParameter[] parameters { get; set; }
            private string UploadDirectory = HttpContext.Current.Server.MapPath("~/App_Data/UploadTemp/");
            private string TempDirectory = HttpContext.Current.Server.MapPath("~/tempFiles/");        
         }

    ReportServerCredentials.cs

        [Serializable]
        public sealed class ReportServerCredentials : IReportServerConnection2//IReportServerCredentials
        {
    
            #region Private Properties
            private string _username;
            private string _password;
            private string _domain;
            #endregion Private Properties
    
            #region Public Properties
            public System.Security.Principal.WindowsIdentity ImpersonationUser
            {
                get { return null; }
            }
    
            public System.Net.ICredentials NetworkCredentials
            {
                get { return new NetworkCredential(_username, _password, _domain); }
            }
            #endregion Public Properties
    
            #region Constructor
            public ReportServerCredentials(string userName, string password, string domain)
            {
                _username = userName;
                _password = password;
                _domain = domain;
            }
            public ReportServerCredentials()
            {
                var appSetting = WebConfigurationManager.AppSettings;
                _username = appSetting["ReportServerUser"];
                _password = appSetting["ReportServerPassword"];
                _domain = appSetting["ReportServerDomain"];
            }
            #endregion Constructor
    
            #region Public Method
            public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
            {
                authCookie = null;
                userName = null;
                password = null;
                authority = null;
                return false;
            }
            #endregion Public Method
    
    
            public IEnumerable<Cookie> Cookies { get { return null; } }
            
    
            public IEnumerable<string> Headers { get { return null; } }
    
            public Uri ReportServerUrl { get { return new Uri(WebConfigurationManager.AppSettings["ReportServerUrl"]); } }
            
    
            public int Timeout { get { return 60000; } }
        }

    you can use like this in controller:

    HomeController.cs
    -----------------
    public class HomeController
    {
    
        public ActionResult ActionReport(int Id)
        {
          ReportingServicesReportViewModel model = new ReportingServicesReportViewModel(
          "ReportPath",
          new List<Microsoft.Reporting.WebForms.ReportParameter>()
          { 
            new Microsoft.Reporting.WebForms.ReportParameter("parameter1",Id.ToString(),false) 
                    });
                return View("ViewReport", model);
            }
    }
    
    ------------------
    ViewReport.cshtml
    ------------------
    
    @model ReportingInfrastructure.ReportingServicesReportViewModel
    <div style="display: table; width: 100%;">
    
        @{
            
            @Html.Partial("ReportViewerControl", Model)
            
        }
    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 23, 2014 6:02 AM

All replies

  • User541108374 posted

    Hi,

    in the past we mixed MVC and webforms for this purpose. Nowadays it's even easier with the One ASP.NET initiative. If you're on older material take a look at http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc.

    Grz, Kris.

    Thursday, January 23, 2014 5:24 AM
  • User421908416 posted

    you need to create the view like ReportView.acs and then you need to create a report View Model, please take a look.

    View: ReportViewer.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ReportViewerControl.ascx.cs" Inherits="ReportViewerControl" %>
    <%@ Register TagPrefix="rsweb" Namespace="Microsoft.Reporting.WebForms" Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
    
    <form id="form1" runat="server">
    <div style="Height:720px;Width:800px">
        <asp:ScriptManager ID="scriptManager" runat="server" ScriptMode="Release" EnablePartialRendering="false" />
        <rsweb:ReportViewer Width="100%" Height="100%" ID="reportViewer" ShowPrintButton="true" KeepSessionAlive="true" runat="server" AsyncRendering="false" ProcessingMode="Remote">
             <ServerReport />
        </rsweb:ReportViewer>
    </div>
    </form>

    ReportViewer.ascx.cs

    namespace 
    {
        public partial class ReportViewerControl : System.Web.Mvc.ViewUserControl
        {
            protected void Page_Init(object sender, EventArgs e)
            {
                // Required for report events to be handled properly.
                //reportViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials();
                Context.Handler = Page;
                
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {                
                    ReportingServicesReportViewModel model = (ReportingServicesReportViewModel)Model;
                    reportViewer.ServerReport.ReportServerCredentials = model.ServerCredentials;
                    ReportParameter[] RptParameters = model.parameters;
                    
                    reportViewer.ServerReport.ReportPath = model.ReportPath;
                    reportViewer.ServerReport.ReportServerUrl = model.ReportServerURL;                
    
                    if(RptParameters.Count() > 0)
                    this.reportViewer.ServerReport.SetParameters(RptParameters);
                    this.reportViewer.ServerReport.Refresh();
                    
                }            
            }
        }   
    }

    ReportingServiceViewModel.cs

        public class ReportingServicesReportViewModel
        {
            #region Constructor
            public ReportingServicesReportViewModel(String reportPath,List<ReportParameter> Parameters)
            {
                ReportPath = reportPath;
                parameters = Parameters.ToArray();
            }
            public ReportingServicesReportViewModel()
            {           
            }
            #endregion Constructor
    
            #region Public Properties
            public ReportServerCredentials ServerCredentials { get { return new ReportServerCredentials(); } }
            public String ReportPath { get; set; }
            public Uri ReportServerURL { get { return new Uri(WebConfigurationManager.AppSettings["ReportServerUrl"]); } }
            public ReportParameter[] parameters { get; set; }
            private string UploadDirectory = HttpContext.Current.Server.MapPath("~/App_Data/UploadTemp/");
            private string TempDirectory = HttpContext.Current.Server.MapPath("~/tempFiles/");        
         }

    ReportServerCredentials.cs

        [Serializable]
        public sealed class ReportServerCredentials : IReportServerConnection2//IReportServerCredentials
        {
    
            #region Private Properties
            private string _username;
            private string _password;
            private string _domain;
            #endregion Private Properties
    
            #region Public Properties
            public System.Security.Principal.WindowsIdentity ImpersonationUser
            {
                get { return null; }
            }
    
            public System.Net.ICredentials NetworkCredentials
            {
                get { return new NetworkCredential(_username, _password, _domain); }
            }
            #endregion Public Properties
    
            #region Constructor
            public ReportServerCredentials(string userName, string password, string domain)
            {
                _username = userName;
                _password = password;
                _domain = domain;
            }
            public ReportServerCredentials()
            {
                var appSetting = WebConfigurationManager.AppSettings;
                _username = appSetting["ReportServerUser"];
                _password = appSetting["ReportServerPassword"];
                _domain = appSetting["ReportServerDomain"];
            }
            #endregion Constructor
    
            #region Public Method
            public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
            {
                authCookie = null;
                userName = null;
                password = null;
                authority = null;
                return false;
            }
            #endregion Public Method
    
    
            public IEnumerable<Cookie> Cookies { get { return null; } }
            
    
            public IEnumerable<string> Headers { get { return null; } }
    
            public Uri ReportServerUrl { get { return new Uri(WebConfigurationManager.AppSettings["ReportServerUrl"]); } }
            
    
            public int Timeout { get { return 60000; } }
        }

    you can use like this in controller:

    HomeController.cs
    -----------------
    public class HomeController
    {
    
        public ActionResult ActionReport(int Id)
        {
          ReportingServicesReportViewModel model = new ReportingServicesReportViewModel(
          "ReportPath",
          new List<Microsoft.Reporting.WebForms.ReportParameter>()
          { 
            new Microsoft.Reporting.WebForms.ReportParameter("parameter1",Id.ToString(),false) 
                    });
                return View("ViewReport", model);
            }
    }
    
    ------------------
    ViewReport.cshtml
    ------------------
    
    @model ReportingInfrastructure.ReportingServicesReportViewModel
    <div style="display: table; width: 100%;">
    
        @{
            
            @Html.Partial("ReportViewerControl", Model)
            
        }
    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 23, 2014 6:02 AM