you need to create a view like Reportview.ascx (that’s right, webform) and then you need to create a report view model, please take a look at.
View: ReportViewer.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ReportViewerControl.ascx.cs" Inherits="ReportViewerControl" %>
<%@ Register TagPrefix="rsweb" Namespace="Microsoft.Reporting.WebForms" Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
<form id="form1" runat="server">
<div style="Height:720px;Width:800px">
<asp:ScriptManager ID="scriptManager" runat="server" ScriptMode="Release" EnablePartialRendering="false" />
<rsweb:ReportViewer Width="100%" Height="100%" ID="reportViewer" ShowPrintButton="true" KeepSessionAlive="true" runat="server" AsyncRendering="false" ProcessingMode="Remote">
<ServerReport />
</rsweb:ReportViewer>
</div>
</form>
in code Behind: ReportViewer.ascx.cs
namespace
{
public partial class ReportViewerControl : System.Web.Mvc.ViewUserControl
{
protected void Page_Init(object sender, EventArgs e)
{
// Required for report events to be handled properly.
//reportViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials();
Context.Handler = Page;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ReportingServicesReportViewModel model = (ReportingServicesReportViewModel)Model;
reportViewer.ServerReport.ReportServerCredentials = model.ServerCredentials;
ReportParameter[] RptParameters = model.parameters;
reportViewer.ServerReport.ReportPath = model.ReportPath;
reportViewer.ServerReport.ReportServerUrl = model.ReportServerURL;
if(RptParameters.Count() > 0)
this.reportViewer.ServerReport.SetParameters(RptParameters);
this.reportViewer.ServerReport.Refresh();
}
}
}
}
You create another file to handle requests to the report server:
ReportingServiceViewModel.cs
public class ReportingServicesReportViewModel
{
#region Constructor
public ReportingServicesReportViewModel(String reportPath,List<ReportParameter> Parameters)
{
ReportPath = reportPath;
parameters = Parameters.ToArray();
}
public ReportingServicesReportViewModel()
{
}
#endregion Constructor
#region Public Properties
public ReportServerCredentials ServerCredentials { get { return new ReportServerCredentials(); } }
public String ReportPath { get; set; }
public Uri ReportServerURL { get { return new Uri(WebConfigurationManager.AppSettings["ReportServerUrl"]); } }
public ReportParameter[] parameters { get; set; }
private string UploadDirectory = HttpContext.Current.Server.MapPath("~/App_Data/UploadTemp/");
private string TempDirectory = HttpContext.Current.Server.MapPath("~/tempFiles/");
}
Configure the credentials:
ReportServerCredentials.cs
[Serializable]
public sealed class ReportServerCredentials : IReportServerConnection2//IReportServerCredentials
{
#region Private Properties
private string _username;
private string _password;
private string _domain;
#endregion Private Properties
#region Public Properties
public System.Security.Principal.WindowsIdentity ImpersonationUser
{
get { return null; }
}
public System.Net.ICredentials NetworkCredentials
{
get { return new NetworkCredential(_username, _password, _domain); }
}
#endregion Public Properties
#region Constructor
public ReportServerCredentials(string userName, string password, string domain)
{
_username = userName;
_password = password;
_domain = domain;
}
public ReportServerCredentials()
{
var appSetting = WebConfigurationManager.AppSettings;
_username = appSetting["ReportServerUser"];
_password = appSetting["ReportServerPassword"];
_domain = appSetting["ReportServerDomain"];
}
#endregion Constructor
#region Public Method
public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
{
authCookie = null;
userName = null;
password = null;
authority = null;
return false;
}
#endregion Public Method
public IEnumerable<Cookie> Cookies { get { return null; } }
public IEnumerable<string> Headers { get { return null; } }
public Uri ReportServerUrl { get { return new Uri(WebConfigurationManager.AppSettings["ReportServerUrl"]); } }
public int Timeout { get { return 60000; } }
}
You can use this controller to call a new view that you will create:
HomeController.cs
public class HomeController
{
public ActionResult ActionReport(int Id)
{
ReportingServicesReportViewModel model = new ReportingServicesReportViewModel(
"ReportPath",
new List<Microsoft.Reporting.WebForms.ReportParameter>()
{
new Microsoft.Reporting.WebForms.ReportParameter("parameter1",Id.ToString(),false)
});
return View("ViewReport", model);
}
}
Here is the view that this controller should point out:
ViewReport.cshtml
@model ReportingInfrastructure.ReportingServicesReportViewModel
<div style="display: table; width: 100%;">
@{
@Html.Partial("ReportViewerControl", Model)
}
</div>
Okay, now just make a call to the controller and should work.
A few weeks ago I wrote an article that shows how to display Report Viewer reports on MVC.. If you still need it, here’s the link: Using Report Viewer in ASP.NET MVC Hug!
– Andre Lima