ASP.NET文件上传示例在IIS局域网环境运行失败问题排查
错误原因
出现无法加载类型 'upload.WebForm1'报错是三个问题共同导致的:
- Page指令属性误用:你当前的IIS站点属于网站项目模型,默认使用动态编译逻辑,而你填写的
Codebehind属性是Web应用程序项目专属的,依赖预编译生成的dll文件,IIS无法通过该属性定位到对应的后台代码类。 - 后台类缺少partial修饰符:ASP.NET Web Forms的页面类是拆分的partial类,一半由开发者编写业务逻辑,另一半由编译器根据前端页面的服务端控件自动生成,缺少partial修饰符会导致类定义不完整,无法被系统识别。
- 后台类成员缺失:空的类定义缺少与前端控件绑定的成员、事件处理逻辑,导致类型加载时校验不通过。
修复方案
对应错误原因修改三处即可:
- 将.aspx页面Page指令中的
Codebehind属性改为CodeFile - 给后台的WebForm1类添加partial修饰符
- 补全后台类的控件绑定、初始化逻辑和上传处理代码
完整可运行代码
WebForm1.aspx
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="upload.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server" EncType="multipart/form-data" action="WebForm1.aspx"> 要上传到服务器的文件:<INPUT id="oFile" type="file" runat="server" NAME="oFile"> <asp:button id="btnUpload" type="submit" text="上传" runat="server"></asp:button> <asp:Panel ID="frmConfirmation" Visible="False" Runat="server"> <asp:Label id="lblUploadResult" Runat="server"></asp:Label> </asp:Panel> </form> </body> </HTML>
WebForm1.aspx.cs
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; namespace upload { /// <summary> /// WebForm1的摘要描述 /// </summary> public partial class WebForm1 : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { // 在此处添加页面初始化代码 } #region Web窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // CODEGEN:ASP.NET Web窗体设计器需要此调用 InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法,请勿使用代码编辑器修改此方法的内容 /// </summary> private void InitializeComponent() { this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void btnUpload_Click(object sender, System.EventArgs e) { string strFileName; string strFilePath; string strFolder; strFolder = Server.MapPath("./"); // 获取上传的文件名 strFileName = oFile.PostedFile.FileName; strFileName = Path.GetFileName(strFileName); if(oFile.Value != "") { // 若存储目录不存在则创建 if(!Directory.Exists(strFolder)) { Directory.CreateDirectory(strFolder); } // 将上传文件保存到服务器 strFilePath = strFolder + strFileName; if(File.Exists(strFilePath)) { lblUploadResult.Text = strFileName + "已存在于服务器中!"; } else { oFile.PostedFile.SaveAs(strFilePath); lblUploadResult.Text = strFileName + "上传成功"; } } else { lblUploadResult.Text = "请点击“浏览”选择要上传的文件"; } // 显示上传结果 frmConfirmation.Visible = true; } } }
内容的提问来源于stack exchange,提问作者Julian
相关产品推荐
相关产品推荐

