Mar 22, 2016

How to display SSRS Report in ASP.NET MVC Application

Today, we will explain how to show SSRS reports in ASP.NET MVC application. Suppose you already created awesome SSRS reports and want to use MVC as Report viewing application. Here is your solution.

Open existing application or create new MVC application. You can install reporting package from NuGet.

Go to Tools -> Library Package Manager -> click on Package Manager Console and type following command into command window.

PM> Install-Package ReportViewerForMvc

OR

Second way, you can add this package from Tools -> Library Package Manager -> Manage NuGet Packages for Solution… window. Search "reportviewer for mvc" in search bar and install.

ssrs asp.net mvc

You have to add one more reference "Microsoft.ReportViewer.WebForms".

When you completed installation of these files, you will automatically get "ReportViewerWebForm.aspx" page on root.

Also you have to update web.config file as below:

1. Add following in <appSettings> Section.


<add key="ReportPath" value="/MyReports/"/>

2. Add following httpHandlers in <system.web> Section same as below


<httpHandlers>
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=12.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXXXX" validate="false"/>
</httpHandlers>

(Please be-aware with the version of .dll file when including above line in web.config file).

3. Add following handlers in <system.webServer> Section same as below:


<handlers>
      <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
</handlers>

4. Add <dependentAssembly> in <assemblyBinding> Section of <runtime> section same as below. (if its already exists then ignore).


<dependentAssembly>
    <assemblyIdentity name="Microsoft.ReportViewer.Common" 
                      publicKeyToken="89845dcd8080cc91"/>
    <bindingRedirect oldVersion="10.0.0.0-12.0.0.0" newVersion="12.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
    <assemblyIdentity name="Microsoft.ReportViewer.WebForms"
                      publicKeyToken="89845dcd8080cc91"/>
    <bindingRedirect oldVersion="10.0.0.0-12.0.0.0" newVersion="12.0.0.0"/>
</dependentAssembly>

Then, create ActionResult with the name of Report and add their view by right clicking on controllerName.


public ActionResult Report()
      {
            ReportViewer rptViewer = new ReportViewer();

            // ProcessingMode will be Either Remote or Local  
            rptViewer.ProcessingMode = ProcessingMode.Remote;
            rptViewer.SizeToReportContent = true;
            rptViewer.ZoomMode = ZoomMode.PageWidth;
            rptViewer.Width = Unit.Percentage(99);
            rptViewer.Height = Unit.Pixel(1000);
            rptViewer.AsyncRendering = true;
            rptViewer.ServerReport.ReportServerUrl = new Uri("http://localhost/ReportServer/");

            rptViewer.ServerReport.ReportPath = this.SetReportPath();

            ViewBag.ReportViewer = rptViewer;
            return View();
}

Then update Report.cshtml view with following.


@if (ViewBag.ReportViewer != null)
{
          @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)
}

Add required using reference if any.

Now hit F5 and see the output of report something like below image

ssrs asp.net mvc

This article explains how to show SSRS report in ASP.NET MVC application and related configuration in web.config. Just try the steps shared and feedback your experience. In case of any doubt or query, use comment box.

Author Bio: James Warner is a highly qualified digital marketing and entrepreneurship. I'm a contributing editor of NexSoftsys for many years in a various role including editor Technology, Health & Media editor and also working as a freelance. You can contact me on Google+.