Saturday, September 6, 2014

Display SSRS Report in MVC using MvcReportViewer


Recently I got a task of Displaying SSRS report in ASP.NET MVC application.

Initially I thought this might not be possible in MVC application(Since web forms have ReportViewer control which is server side control requiring view state and which is not possible in MVC) and I would have to do some work around of mixing MVC and ASP.NET Web Forms pages to view report.

Later on I searched some more and found this solution. So let us start with it.


I created a BI solution and created a simple report(named EmpReport) as shown below.
I deployed this solution to my local SSRS server. Now my requirement is to display this deployed report in my MVC application.
Sample SSRS report Designed in BIDS















I Created a MVC project(named Mvc_SSRS_Demo) where I would like to integrate this report.

We would install "MvcReportViewer" package from NuGet.

To install it open 'Package Manager Console' from Visual Studio Tools --> Library Package Manager --> Package Manager Console 
and Run following command in 'Package Manager Console'
Install-Package MvcReportViewer
NuGet Package Manager Console

This will add packages for MvcReportViewer library and will add some default configurations in web.config

We will have to update following entries in web.config

<add key="MvcReportViewer.AspxViewer" value="/MvcReportViewer.aspx" />
         => This points to aspx page which is installed by NuGet package which will be used to display our report in iframe. If you move this page to some other location or rename it then you will have to update that change here.

<add key="MvcReportViewer.ReportServerUrl" value="http://naren-pc/Reportserver" />
=> This points to SSRS server where our report is deployed. Change this value to point it to your report server.

<add key="MvcReportViewer.Username" value="" />
<add key="MvcReportViewer.Password" value="" />
=> Provide credentials if required to connect to your report server.

Now to use this Report Viewer in your view Reference it as follows
@using MvcReportViewer

Now use following HTML helper method to load required report
@Html.MvcReportViewer(
    "/MvcSSRS_Integration/EmpReport",
    new { EmpID = 1 },
    new { Height = 600, Width = 600, style = "border: none" })
This helper method has following parameters
1. Relative path of SSRS report on server
2. Required Parameters for report execution
3. Styling and other settings for report viewer

Now Run your application and navigate to this View and it will render your SSRS report in your MVC application. Pretty cool :)

Hope this makes it clear and helps you.