绑定RDLC Report Viewer时出现Server could not create ASP页面创建异常求助
绑定RDLC Report Viewer时出现"Server could not create ASP.reporting_reportvalueprediction_aspx"异常的解决办法
错误信息
当前Web请求执行期间发生未处理异常,请查看堆栈跟踪获取错误详情及代码来源。
异常详情:System.InvalidOperationException: Server could not create ASP.reporting_reportvalueprediction_aspx。
堆栈跟踪
[InvalidOperationException: Server could not create ASP.reporting_reportvalueprediction_aspx.] __ASP.FastObjectFactory_app_web_1r4mqrnz.Create_ASP_reporting_reportvalueprediction_aspx() +192 System.Web.Compilation.BuildResultCompiledType.CreateInstance() +31 System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) +104 System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33 System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String requestType, String virtualPath, String path) +39 System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +386 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
尝试的代码
ASPX页面代码
<%@ Page Language="vb" Debug="true" AutoEventWireup="false" CodeBehind="ReportValuePrediction.aspx.vb" Inherits="TimiWeb.ReportValuePrediction" %> <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager" runat="server"> </asp:ScriptManager> <div> <rsweb:ReportViewer ID="rptReportViewer" runat="server" Width="100%" Height="700px"> </rsweb:ReportViewer> </div> </form> </body> </html>
VB后台代码
Imports Microsoft.Reporting.WebForms Public Class ReportValuePrediction Inherits System.Web.UI.Page Private _dt As DataTable Public Sub New(ByVal dt As DataTable) _dt = dt End Sub Protected Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then Dim rep As LocalReport = rptReportViewer.LocalReport rep.ReportPath = "/Reporting/ValuePrediction.rdlc" Dim rds As New ReportDataSource() If _dt.Rows.Count > 0 Then rds.Name = "ValuePredictionDS" rds.Value = _dt rep.DataSources.Add(rds) End If End If End Sub End Class
问题原因与解决方案
问题根源
ASP.NET Web Forms的页面类必须提供无参数的默认构造函数,因为ASP.NET运行时会通过无参构造来实例化页面对象。你自定义了带DataTable参数的构造函数,导致运行时无法创建页面实例,进而抛出异常。
修改步骤
- 删除带参数的构造函数,确保页面类保留默认无参构造(可以显式写出,也可依赖编译器自动生成)
- 改用Session、QueryString或直接从数据源加载的方式传递
DataTable,而非通过构造函数传递
修改后的后台代码示例
Imports Microsoft.Reporting.WebForms Public Class ReportValuePrediction Inherits System.Web.UI.Page Private _dt As DataTable ' 显式声明无参构造函数(可选,编译器会自动生成) Public Sub New() End Sub Protected Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then ' 示例:从Session获取预先存储的DataTable _dt = DirectCast(Session("PredictionData"), DataTable) Dim rep As LocalReport = rptReportViewer.LocalReport rep.ReportPath = "/Reporting/ValuePrediction.rdlc" Dim rds As New ReportDataSource() If _dt IsNot Nothing AndAlso _dt.Rows.Count > 0 Then rds.Name = "ValuePredictionDS" rds.Value = _dt rep.DataSources.Add(rds) End If End If End Sub End Class
额外注意事项
- 若使用Session传递数据,需确保在跳转到该页面之前将
DataTable存入Session,并注意Session的生命周期设置 - 确认项目已正确安装
Microsoft.ReportViewer.WebFormsNuGet包,引用版本与页面中注册的一致 - 检查RDLC文件路径是否正确,确保文件存在且属性设置为"如果较新则复制"
内容的提问来源于stack exchange,提问作者Arsh
相关产品推荐
相关产品推荐

