Introduction
There is no control for Report Viewer in MVC, so we cannot view a report using MVC applications. Most business applications have report requirements. In article, I will explain the work around to make it happen. Here I will assume that we already have a report server with some test report.
Report server folder structure:
MVC does not provide a Report Viewer control so we need to add a classic web form page to our MVC application. The recommendation is to place this form outside of the view folder so that we don't need to register or ignore the route.
In this classic web form page, we need to add the following two controls:
- Script manger
- Report Viewer control
ReportViewer.aspx code
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
- <div>
- <rsweb:reportviewer id="rptViewer" runat="server" height="503px" width="663px">
- </rsweb:reportviewer>
- </div>
- </form>
The following rocedure is required to write a report render function:
- Set processing mode to remote.
- Pass report server URL and report path.
- Pass the parameters if any.
- Pass Credential of Report Server if required.
- Refresh the report.
The following is the show report function:
- private void ShowReport()
- {
- try
- {
-
- string urlReportServer = "http://Servername/Reportserver";
-
-
- rptViewer.ProcessingMode = ProcessingMode.Remote;
-
-
- rptViewer.ServerReport.ReportServerUrl = new Uri(urlReportServer);
-
-
-
- rptViewer.ServerReport.ReportPath = "/Jignesh/TestReport";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- rptViewer.ServerReport.Refresh();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
In the page load event of the Report Viewer page, call the preceding function to render the report. In this example, we are passing the hardcoded report name. Instead of this we can pass the report name using any state management technique.
Report Viewer page Code:
- public partial class ReportViewer : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- ShowReport();
- }
- }
Now we need to call this view from our controllers. For that I have created an ActionResult and called it from the menu.
Controller Code:
- public ActionResult ShowReport()
- {
- return Redirect("../ReportViewer/ReportViewer.aspx");
- }
Output:
Summary
Using this work around, we are able to view a SSRS report in a MVC application. I hope you enjoy the article.