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

17 comments

  1. if you are facing error while loading control. You need to uninstall all existing “Microsoft.ReportViewer.WebForms” dll using Nuget and add the mentioned chaos “ReportViewerForMVC” .This will add report viewer internally and reslove the assembly issues.

  2. My report is taking almost 30-40 seconds to load it on SSRS portal , but in MVC Webportal the form submission is getting finished in 5-7 seconds without rendering anything on iframe . I tried to change the settings of aspx page Buffer=”false” , but still no success.
    Also in web config i modified below things and no success

    It would be really helpful if get some thoughts on this issue and how we can sort this.

    Between the small reports which are taking less time to load are rendering quickly are loading in web portal without issues. So is it about some Web config settings or some other settings that needs to be done for loading bigger reports

  3. Hi I am getting error : Server Error in ‘/’ Application.

    Could not load file or assembly ‘Microsoft.ReportViewer.ProcessingObjectModel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IO.FileLoadException: Could not load file or assembly ‘Microsoft.ReportViewer.ProcessingObjectModel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Assembly Load Trace: The following information can be helpful to determine why the assembly ‘Microsoft.ReportViewer.ProcessingObjectModel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’ could not be loaded.

    WRN: Assembly binding logging is turned OFF.
    To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
    Note: There is some performance penalty associated with assembly bind failure logging.
    To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

    Stack Trace:

    [FileLoadException: Could not load file or assembly ‘Microsoft.ReportViewer.ProcessingObjectModel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
    Microsoft.ReportingServices.RdlExpressions.ExprHostCompiler.InternalCompile(AppDomain compilationTempAppDomain, Boolean refusePermissions) +0
    Microsoft.ReportingServices.RdlExpressions.c__DisplayClass2.b__0() +19
    Microsoft.ReportingServices.Diagnostics.c__DisplayClass1.b__0(Object state) +116
    System.Security.SecurityContext.Run(SecurityContext securityContext, ContextCallback callback, Object state) +13063821
    Microsoft.ReportingServices.Diagnostics.RevertImpersonationContext.Run(ContextBody callback) +99
    Microsoft.ReportingServices.RdlExpressions.ExprHostCompiler.Compile(IExpressionHostAssemblyHolder expressionHostAssemblyHolder, AppDomain compilationTempAppDomain, Boolean refusePermissions, PublishingVersioning versioning) +184
    Microsoft.ReportingServices.ReportPublishing.ReportPublishing.Phase3(ParameterInfoCollection& parameters, Dictionary`2& groupingExprCountAtScope) +1445
    Microsoft.ReportingServices.ReportPublishing.ReportPublishing.InternalCreateIntermediateFormat(Stream definitionStream, String& description, String& language, ParameterInfoCollection& parameters, DataSourceInfoCollection& dataSources, DataSetInfoCollection& sharedDataSetReferences, UserLocationFlags& userReferenceLocation, ArrayList& dataSetsName, Boolean& hasExternalImages, Boolean& hasHyperlinks, Byte[]& dataSetsHash) +125
    Microsoft.ReportingServices.ReportPublishing.ReportPublishing.CreateIntermediateFormat(Byte[] definition, String& description, String& language, ParameterInfoCollection& parameters, DataSourceInfoCollection& dataSources, DataSetInfoCollection& sharedDataSetReferences, UserLocationFlags& userReferenceLocation, ArrayList& dataSetsName, Boolean& hasExternalImages, Boolean& hasHyperlinks, Byte[]& dataSetsHash) +82
    Microsoft.ReportingServices.ReportProcessing.ReportProcessing.CompileOdpReport(PublishingContext reportPublishingContext, IDataProtection dataProtection, PublishingErrorContext errorContext, String& reportDescription, String& reportLanguage, ParameterInfoCollection& parameters, DataSourceInfoCollection& dataSources, DataSetInfoCollection& sharedDataSetReferences, UserLocationFlags& userReferenceLocation, ArrayList& dataSetsName, Boolean& hasExternalImages, Boolean& hasHyperlinks, Byte[]& dataSetsHash) +165
    Microsoft.ReportingServices.ReportProcessing.ReportProcessing.CreateIntermediateFormat(PublishingContext reportPublishingContext, IDataProtection dataProtection) +204

    [ReportProcessingException: An unexpected error occurred in Report Processing.]
    Microsoft.ReportingServices.ReportProcessing.ReportProcessing.CreateIntermediateFormat(PublishingContext reportPublishingContext, IDataProtection dataProtection) +997
    Microsoft.Reporting.ReportCompiler.CompileReport(ICatalogItemContext context, Byte[] reportDefinition, Boolean generateExpressionHostWithRefusedPermissions, ControlSnapshot& snapshot) +203

    [DefinitionInvalidException: The definition of the report ‘C:\Users\shubham.choudhary\source\repos\WebApplication1\WebApplication1\Reports\TestReport.rdlc’ is invalid.]
    Microsoft.Reporting.ReportCompiler.CompileReport(ICatalogItemContext context, Byte[] reportDefinition, Boolean generateExpressionHostWithRefusedPermissions, ControlSnapshot& snapshot) +269
    Microsoft.Reporting.LocalService.GetCompiledReport(PreviewItemContext itemContext, Boolean rebuild, ControlSnapshot& snapshot) +228
    Microsoft.Reporting.LocalService.CompileReport() +29
    Microsoft.Reporting.LocalService.Microsoft.Reporting.ILocalProcessingHost.CompileReport() +5
    Microsoft.Reporting.WebForms.LocalReport.EnsureExecutionSession() +61

    [LocalProcessingException: An error occurred during local report processing.]
    Microsoft.Reporting.WebForms.LocalReport.EnsureExecutionSession() +150
    Microsoft.Reporting.WebForms.LocalReport.GetParameters() +48
    ReportViewerForMvc.ReportViewerExtensions.SetProperties(LocalReport localReport, LocalReport properties) +80
    ReportViewerForMvc.ReportViewerExtensions.SetProperties(ReportViewer reportViewer, ReportViewer properties) +95
    ReportViewerForMvc.ReportViewerWebForm.BuildReportViewer() +67
    ReportViewerForMvc.ReportViewerWebForm.Page_Load(Object sender, EventArgs e) +5
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +85
    System.Web.UI.Control.OnLoad(EventArgs e) +79
    System.Web.UI.Control.LoadRecursive() +130
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2845

    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0

  4. Hi,
    Thanks For this Article….

    Can u please tell me how to create a DrillDown Report Using asp.Net MVC 5,

    I am Trying to generate DrillDown action Through

    reportViewer.Drillthrough += reportViewer_Drillthrough;

    and generates a Method
    void reportViewer_Drillthrough(object sender, DrillthroughEventArgs e)
    {
    }

    But am trying to raise a break Point it is not raising

    Can anybody Please Help me

  5. Hi Guys,
    Big thanks to James for sharing this article.
    As for the missing reference error on the Razor View, I just had to add the following line at the very top of my razor view:

    @using ReportViewerForMvc

    Best Wishes & Good Luck!

Leave a Reply to Andreas Savva Cancel reply

Your email address will not be published. Required fields are marked *