How To Display SSRS Report In ASP.NET MVC Web Application

Background

You might be aware that it’s easy to incorporate SSRS reports with ASP.NET web application because there’s a server control “ReportViewer” available in ASP.NET. But, integrating the SSRS reports with ASP.NET MVC web application is slightly more complicated. In this article, I will show how to integrate it easily in few steps. This article is for developers having experience of working on ASP.NET MVC web application and some knowledge on SSRS.

Introduction

In this article, I will show how to display an SSRS report in ASP.NET MVC application. For this demo, I am using Visual Studio 2012, ASP.NET MVC 4 - Empty Template, an existing SSRS report deployed on SSRS Server, and a nuGet package. I will be using a nuGet package called ReportViewer for MVC.

ReportViewer for MVC is a .NET project that makes it possible to use an ASP.NET ReportViewer control into an MVC web application. It provides a set of HTML Helpers and a simple ASP.NET Web Form for displaying the ReportViewer within an auto-resized iframe tag.

Getting Started

For integrating the SSRS report in ASP.NET MVC web application, you need some information related to SSRS server handy. You need the following details:

  • SSRS Server URL
  • SSRS folder path
  • Report name – In demo the Report name is Performance.rdl

I have created a demo ASP.NET MVC web application having 2 Views.

  • Home/Index View - displays the list of reports. By clicking on the report link, it will be directed to the reports template for displaying the report.
  • Report/ReportTemplate View - displays the requested report.

Below is the structure of the ASPNETMVC_SSRS_Demo project. As you can see in the Solution Explorer, under Controller folder, I have HomeController and ReportController, and under the View folder, I have the Home folder with Index View and Report folder with ReportTemplate View.

solution

Next step is Installing the reporting package - ReportViewer for MVC from nuGet. This is the most important step. You can install any nuGet package using any one of the following ways.

  • Using the  Package Manager console
    or 
  • By right clicking on the project in Solution Explorer and selecting the option Manage NuGet package for solution.

I am showing you steps for installing the ReportViewerForMvc using the Package Manager console.

Using Package Manager console

Click on Tools -> Nuget Package Manager.

Tools

The Package Manager console will open.

package manager console

Next, type the below command at the prompt PM> Install-package ReportViewerForMvc and press enter. After few minutes, the package will be installed.

Install-package ReportViewerForMvc

This installation will add to the project: 2 assemblies (Microsoft.ReportViewer.WebForms & ReportViewerForMvc) to reference an ASPX page (ReportViewerWebForm.aspx) and httphandlers settings in the web.config file.

Note: The ASPX page added does not have a .cs file.

cs file

cs file

cs file

You can now use this ASPX page and code everywhere in controller (but I am using a slightly different path for code reusability and consistency).

Add a new folder ‘Reports” to the project, and then, add a new webform .aspx page ReportTemplate.aspx to the Reports folder.

Reports

Copy the contents (as shown in fig) from ReportViewerWebForm.aspx and replace the content of ReportTemplate.aspx with this.

Note: Please do not copy @page directive, copy only the highlighted section.

page directive

  1. <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"  
  2.     Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>  
  3.    
  4. <%--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">--%>  
  5. <!doctype html>  
  6.  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11">  
  7.    
  8. <html xmlns="http://www.w3.org/1999/xhtml">  
  9. <head id="Head1" runat="server">  
  10.     <title></title>  
  11.       
  12. </head>  
  13. <body>  
  14.     <form id="form1" runat="server">  
  15.     <div>  
  16.         <asp:ScriptManager ID="scriptManagerReport" runat="server">  
  17.          </asp:ScriptManager>  
  18.    
  19.         <rsweb:ReportViewer  runat ="server" ShowPrintButton="false"  Width="99.9%" Height="100%" AsyncRendering="true" ZoomMode="Percent" KeepSessionAlive="true" id="rvSiteMapping" SizeToReportContent="false" >  
  20.         </rsweb:ReportViewer>                    
  21.     </div>  
  22.     </form>  
  23. </body>  
  24. </html>  
The ReportTemplate.aspx will change to this:

ReportTemplate
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportTemplate.aspx.cs" Inherits="ASPNETMVC_SSRS_Demo.Reports.ReportTemplate" %>  
  2.    
  3. <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"  
  4.     Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>  
  5.    
  6. <%--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">--%>  
  7. <!doctype html>  
  8.  <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11">  
  9.    
  10. <html xmlns="http://www.w3.org/1999/xhtml">  
  11. <head id="Head1" runat="server">  
  12.     <title></title>  
  13.       
  14. </head>  
  15. <body>  
  16.     <form id="form1" runat="server">  
  17.     <div>  
  18.         <asp:ScriptManager ID="scriptManagerReport" runat="server">  
  19.  <Scripts>  
  20.      <asp:ScriptReference Assembly="ReportViewerForMvc" Name="ReportViewerForMvc.Scripts.PostMessage.js" />  
  21.   </Scripts>  
  22.          </asp:ScriptManager>  
  23.    
  24.         <rsweb:ReportViewer  runat ="server" ShowPrintButton="false"  Width="99.9%" Height="100%" AsyncRendering="true" ZoomMode="Percent" KeepSessionAlive="true" id="rvSiteMapping" SizeToReportContent="false" >  
  25.         </rsweb:ReportViewer>                    
  26.     </div>  
  27.     </form>  
  28. </body>  
  29. </html>  
Next, delete the below scripts tag from ReportTemplate.aspx page.
  1. <Scripts>  
  2.    <asp:ScriptReference Assembly="ReportViewerForMvc" Name="ReportViewerForMvc.Scripts.PostMessage.js" />  
  3. </Scripts>  
Add additional attributes to the ReportViewercontrol, as shown below.
  1. <rsweb:ReportViewer id="rvSiteMapping" runat ="server" ShowPrintButton="false"  Width="99.9%" Height="100%" AsyncRendering="true" ZoomMode="Percent" KeepSessionAlive="true" SizeToReportContent="false" ></rsweb:ReportViewer>    
Now, open ReportTemplate.aspx.cs file and add the following code to the Page_load event. You need the SSRS Server URL and SSRS report folder path.
  1. using System.Web;  
  2. using System.Web.UI;  
  3. using System.Web.UI.WebControls;  
  4.    
  5. namespace ASPNETMVC_SSRS_Demo.Reports  
  6. {  
  7.     public partial class ReportTemplate : System.Web.UI.Page  
  8.     {  
  9.         protected void Page_Load(object sender, EventArgs e)  
  10.         {  
  11.             if (!IsPostBack)  
  12.             {  
  13.                 try  
  14.                 {  
  15.                     String reportFolder = System.Configuration.ConfigurationManager.AppSettings["SSRSReportsFolder"].ToString();  
  16.    
  17.                     rvSiteMapping.Height = Unit.Pixel(Convert.ToInt32(Request["Height"]) - 58);  
  18.                     rvSiteMapping.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;  
  19.    
  20.                     rvSiteMapping.ServerReport.ReportServerUrl = new Uri("SSRS URL"); // Add the Reporting Server URL  
  21.                     rvSiteMapping.ServerReport.ReportPath = String.Format("/{0}/{1}", reportFolder, Request["ReportName"].ToString());  
  22.    
  23.                     rvSiteMapping.ServerReport.Refresh();  
  24.                 }  
  25.                 catch (Exception ex)  
  26.                 {  
  27.                       
  28.                 }  
  29.             }  
  30.         }  
  31.     }  
  32. }  
code

Add the SSRSReportFolder path to the app settings in the web.config file.
  1. <add key="SSRSReportsFolder" value="BIC_Reports"/>  
settings

Next, create an entity class ReportInfo.cs under Models folder.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.    
  6. namespace ASPNETMVC_SSRS_Demo.Models  
  7. {  
  8.     public class ReportInfo  
  9.     {  
  10.         public int ReportId { getset; }  
  11.         public string ReportName { getset; }  
  12.         public string ReportDescription { getset; }  
  13.         public string ReportURL { getset; }  
  14.         public int Width { getset; }  
  15.         public int Height { getset; }  
  16.         public string ReportSummary { getset; }  
  17.     }  
  18.    
  19. }  
models

Next, we will add code to the Controller and the View pages. There is no change to the HomeController.cs. Add the following code to Home/Index View page.
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.    
  5. <h2>Reports List</h2>  
  6.    
  7. <a id="ReportUrl_Performance" href="@Url.Action("ReportTemplate", "Report", new { ReportName = "Performance", ReportDescription = "Performance Report", Width = 100, Height = 650 })">  
  8.     Performance Report</a>  
code

Next, add ActionResult ReportTemplate to the Report Controller.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using ASPNETMVC_SSRS_Demo.Models;  
  7.    
  8. namespace ASPNETMVC_SSRS_Demo.Controllers  
  9. {  
  10.     public class ReportController : Controller  
  11.     {  
  12.         //  
  13.         // GET: /Report/  
  14.    
  15.       public ActionResult ReportTemplate(string ReportName, string ReportDescription, int Width, int Height)  
  16.         {  
  17.             var rptInfo = new ReportInfo  
  18.             {  
  19.                 ReportName = ReportName,  
  20.                 ReportDescription = ReportDescription,  
  21.                 ReportURL = String.Format("../../Reports/ReportTemplate.aspx?ReportName={0}&Height={1}", ReportName, Height),  
  22.                 Width = Width,  
  23.                 Height = Height  
  24.             };  
  25.    
  26.             return View(rptInfo);  
  27.         }  
  28.    
  29.    
  30.     }  
  31. }  
code

The final step is to open the ReportTemplate View page under Report, and add the following code.
  1. @model ASPNETMVC_SSRS_Demo.Models.ReportInfo  
  2.    
  3. <H1>  
  4.     @Model.ReportDescription  
  5. </H1>  
  6.    
  7. <iframe id="frmReport" src="@Model.ReportURL" frameborder="0" style="@String.Format("width:{0}%; height: {1}px;", Model.Width, Model.Height)" scrolling="no">  
  8.    
  9. </iframe>  
report

Press F6 to build the application and then press F5 to run the application. It will display the Home / index page.

application

Click on the Performance Report link and there you go. The SSRS report is displayed.

Performance Report

Up Next
    Ebook Download
    View all
    Learn
    View all