Click here to Skip to main content
15,886,036 members
Articles / Web Development / ASP.NET

How to Display SSRS Report in ASP.NET MVC Web Application

Rate me:
Please Sign up or sign in to vote.
4.88/5 (10 votes)
30 Aug 2016CPOL4 min read 48.9K   58   11   7
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.

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. This article is developers having experience working on ASP.NET MVC web application and some knowledge on SSRS.

Background

As most of you might be aware that it’s easy to incorporate SSRS reports with ASP.NET web application, as there’s a server control “ReportViewer” available in ASP.NET, but integrating SSRS reports with ASP.NET MVC web application it’s slightly complicated. In this article I will show how to integrate it easily in few steps. I will be using a nuGet package called ReportViewer for MVC. ReportViewerForMVc

ReportViewer for MVC is a .NET project that makes 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. References: For more information on the “ReportViewer for MVC” project and the actual source code, please visit wiki page on Codeplex

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 HomeComtroller and ReportController, and under the View folder I have the Home folder with Index view and Report folder with ReportTemplate view.

Image 1

Next steps 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 way.

  1. Using the package manager console

    OR

  2. 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.

Image 2

The package manager console will open

Image 3

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

Image 4

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

Note: the aspx page added does not have a .cs file,

Image 5

Image 6

Image 7

You can now use this .aspx page and code everything 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

Image 8

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.

Image 9

<%@ 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">--%>
<!doctype html>
 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="scriptManagerReport" runat="server">
         </asp:ScriptManager>
 
        <rsweb:ReportViewer  runat ="server" ShowPrintButton="false"  Width="99.9%" Height="100%" AsyncRendering="true" ZoomMode="Percent" KeepSessionAlive="true" id="rvSiteMapping" SizeToReportContent="false" >
        </rsweb:ReportViewer>                  
    </div>
    </form>
</body>
</html>

The ReportTemplate.aspx will change to this

Image 10

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportTemplate.aspx.cs" Inherits="ASPNETMVC_SSRS_Demo.Reports.ReportTemplate" %>
 
<%@ 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">--%>
<!doctype html>
 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="scriptManagerReport" runat="server">
 <Scripts>
     <asp:ScriptReference Assembly="ReportViewerForMvc" Name="ReportViewerForMvc.Scripts.PostMessage.js" />
  </Scripts>
         </asp:ScriptManager>
 
        <rsweb:ReportViewer  runat ="server" ShowPrintButton="false"  Width="99.9%" Height="100%" AsyncRendering="true" ZoomMode="Percent" KeepSessionAlive="true" id="rvSiteMapping" SizeToReportContent="false" >
        </rsweb:ReportViewer>                  
    </div>
    </form>
</body>
</html>

Next delete the below scripts tag from ReportTemplate.aspx page

<Scripts>
     <asp:ScriptReference Assembly="ReportViewerForMvc" Name="ReportViewerForMvc.Scripts.PostMessage.js" />
</Scripts>

Add additional attributes to the ReportViewercontrol as below

<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 following code to the Page_load event, you need to the SSRS Server URL and SSRS report Folder path.

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace ASPNETMVC_SSRS_Demo.Reports
{
    public partial class ReportTemplate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    String reportFolder = System.Configuration.ConfigurationManager.AppSettings["SSRSReportsFolder"].ToString();
 
                    rvSiteMapping.Height = Unit.Pixel(Convert.ToInt32(Request["Height"]) - 58);
                    rvSiteMapping.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
 
                    rvSiteMapping.ServerReport.ReportServerUrl = new Uri("SSRS URL"); // Add the Reporting Server URL
                    rvSiteMapping.ServerReport.ReportPath = String.Format("/{0}/{1}", reportFolder, Request["ReportName"].ToString());
 
                    rvSiteMapping.ServerReport.Refresh();
                }
                catch (Exception ex)
                {
                    
                }
            }
        }
    }
}

Image 11

add the SSRSReportFolder path to the app settings in the web.config file.

<add key="SSRSReportsFolder" value="BIC_Reports"/>

Image 12

Next create an entity class ReportInfo.cs under Models folder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace ASPNETMVC_SSRS_Demo.Models
{
    public class ReportInfo
    {
        public int ReportId { get; set; }
        public string ReportName { get; set; }
        public string ReportDescription { get; set; }
        public string ReportURL { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public string ReportSummary { get; set; }
    }
 
}

Image 13

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.

@{
    ViewBag.Title = "Index";
}
 
<h2>Reports List</h2>
 
<a id="ReportUrl_Performance" href="@Url.Action("ReportTemplate", "Report", new { ReportName = "Performance", ReportDescription = "Performance Report", Width = 100, Height = 650 })">
    Performance Report</a>

Image 14

Next add ActionResult ReportTemplate to the Report Controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASPNETMVC_SSRS_Demo.Models;
 
namespace ASPNETMVC_SSRS_Demo.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/
 
      public ActionResult ReportTemplate(string ReportName, string ReportDescription, int Width, int Height)
        {
            var rptInfo = new ReportInfo
            {
                ReportName = ReportName,
                ReportDescription = ReportDescription,
                ReportURL = String.Format("../../Reports/ReportTemplate.aspx?ReportName={0}&Height={1}", ReportName, Height),
                Width = Width,
                Height = Height
            };
 
            return View(rptInfo);
        }
 
 
    }
}

Image 15

Final Step is to open the ReportTemplate view page under Report and add the following code.

@model ASPNETMVC_SSRS_Demo.Models.ReportInfo
 
<H1>
    @Model.ReportDescription
</H1>
 
<iframe id="frmReport" src="@Model.ReportURL" frameborder="0" style="@String.Format("width:{0}%; height: {1}px;", Model.Width, Model.Height)" scrolling="no">
 
</iframe>

Image 16

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

Image 17

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

Image 18

Hope you like this article and please add comments and share your feedback.

This article was posted on my web site Hussain Patel

References:

For more information see the wiki page on codeplex Codeplex

Also you can read more about ReportViewer for Mvc ReportViewerForMVc

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
United States United States
Husband, Father, TheWorkingProgrammer

Comments and Discussions

 
QuestionHow to Display POWER BI report Server Reports in ASP.NET MVC Web Application Pin
Hanan Chrourou21-Jul-22 4:46
Hanan Chrourou21-Jul-22 4:46 
QuestionSomeone know how can i show the report in modal without layout template Pin
julianccortes19-Jun-18 8:59
julianccortes19-Jun-18 8:59 
QuestionI am getting the Error: The request failed with HTTP status 404: Not Found. Pin
Member 1028679118-Oct-17 6:34
Member 1028679118-Oct-17 6:34 
AnswerRe: I am getting the Error: The request failed with HTTP status 404: Not Found. Pin
Hussain Patel20-Oct-17 14:05
professionalHussain Patel20-Oct-17 14:05 
QuestionReportViewer 14.0.0.0- Can't see the report on the screen in the iframe Pin
Member 1205356824-May-17 13:16
Member 1205356824-May-17 13:16 
Question'Microsoft.ReportViewer.Common, Version=11.0.0.0, Pin
Member 1048416230-Nov-16 16:48
Member 1048416230-Nov-16 16:48 
AnswerRe: 'Microsoft.ReportViewer.Common, Version=11.0.0.0, Pin
Hussain Patel30-Nov-16 19:04
professionalHussain Patel30-Nov-16 19:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.