Tuesday, 3 July 2012

ASP.NET MVC and SSRS Reports

To display a SQL Server Report (SSRS) into your website, you will need a ReportViewer server control which belongs to ASP.NET Web Forms world and not ASP.NET MVC. So how can you have a SSRS report into your ASP.NET MVC application? It is easy and thanks to the side-by-side running of these two different worlds!

First, you need a normal Web Form page in your MVC application to put the ReportViewer server control into it. I would create a Reports folder in the root of my MVC application and then add a Web Form called ReportViwer.aspx into it.

Then, drag a ReportViewer server control from the Reports Toolbox into the page.

<%@ Page Language="C#" AutoEventWireup="true"  MasterPageFile="~/Site.Master" CodeBehind="ReportViewer.aspx.cs" Inherits="YourWebsite.Reports.ReportViewer" %>
<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Height="800px" 
            ProcessingMode="Remote" Width="100%" Font-Names="Verdana" Font-Size="8pt" 
                InteractiveDeviceInfos="(Collection)" WaitMessageFont-Names="Verdana" 
                WaitMessageFont-Size="14pt">
        </rsweb:ReportViewer>
</asp:Content>

Next, you need to add a new route to your routing configuration in the Global.asax.

public static void RegisterRoutes(RouteCollection routes)
{
    // Your other route settings
    // ...
 
    routes.MapPageRoute(
        "reports_browse",
        "Report/{reportName}.aspx",
        "~/Reports/ReportViewer.aspx"
        );
 
    // Your other route settings
    // ...
}
In this route, notice to the {reportName} placeholder which will be your actual SSRS report name (reportName.rdl). Here, all we are doing is to send all http://YourApp/Reports/reportName.aspx to the single page ReportViewer.aspx in the Reports folder that we discussed above.
The last step is to read the report name from the route data in ReportViewer.aspx page and set your ReportViewer server control to point to your report on the report server.

protected void Page_Load(object sender, EventArgs e)
{
    string reportName = Page.RouteData.Values["reportName"as string;
    ReportViewer1.ServerReport.ReportPath = String.Format("YourReportsPath/{0}.rdl", reportName);
}

Happy reporting!

4 comments:

  1. the route settings are not working can you please ellaborate a bit how this is done

    ReplyDelete
  2. Are you able to receive *.aspx requests into your application? If no, then you may need to check your IIS settings and see how it is handling aspx requests.

    ReplyDelete
    Replies
    1. can you explain your answer Are you able to receive *.aspx requests into your application?

      Delete
    2. Explain: are you able to process requests ending with .aspx? or is ASP.NET worker process is able to receive and run your .aspx requests in the IIS? I hope it is clear.

      Delete