You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio无法自动更新WebForms的.designer.cs文件求助

WebForms SDK风格项目在Visual Studio中无法自动更新.designer.cs文件的解决办法

我们将一批遗留WebForms项目升级为SDK风格的.csproj文件,目的是统一项目格式、解决绑定重定向问题,为后续WebForms迁移做准备。目前用Rider编辑.aspx文件时,.designer.cs文件能正常自动更新,但在Visual Studio中,该自动更新功能失效。

我推测Rider是通过后台调用相关工具完成代码生成的。如果Visual Studio对SDK风格项目原生不支持该功能,或许可手动添加预构建动作,但暂不清楚具体调用的程序。


示例说明

项目名为ClientInterface,以根目录下的ErrorPage.aspx为例:

.aspx文件(精简后)

<%@ Page Language="C#" EnableSessionState="False" AutoEventWireup="true" CodeBehind="ErrorPage.aspx.cs" Inherits="ClientInterface.ErrorPage" %>
<%@Register tagPrefix="css" tagName="ClientInterface" src="ClientInterfaceCss.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body id="NewStylesPageBody" class="ErrorScreen">
    <form id="form" runat="server">
        <div class="GreenGradientScreen">
            <div class="ErrorBox">
                <div class="ErrorHeaderBox">
                    <asp:Image runat="server" ID="Image" ImageUrl="~/Images/Controls/header-bar-logo.png?new-logo" />
                    <asp:Image runat="server" ID="ErrorIcon" ImageUrl="~/Images/red-error.png" />
                </div>
            </div>
        </div>
    </form>
</body>
</html>

CodeBehind文件(ErrorPage.aspx.cs)

using System;
using System.Web.UI;

namespace ClientInterface
{
    public partial class ErrorPage : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

CodeBehind文件属性

codebehind properties


解决方案

方法1:手动触发设计器生成

在Visual Studio中选中目标.aspx文件,右键选择转换为Web应用程序,可手动触发.designer.cs文件更新。此为单次操作,每次修改控件后需重复执行。

方法2:添加预构建事件自动生成

Visual Studio的WebForms设计器生成工具为aspnet_compiler.exe,可通过预构建事件实现自动更新:

  1. 右键项目 → 属性 → 生成事件 → 预生成事件命令行
  2. 添加以下命令(根据项目路径调整参数):
"%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe" -v /ClientInterface -p "$(ProjectDir)" -c

参数说明:

  • -v:指定项目虚拟路径,此处对应项目名ClientInterface
  • -p:指定项目物理路径,$(ProjectDir)为Visual Studio内置宏,代表当前项目目录
  • -c:完整重新生成,确保所有.designer.cs文件同步更新

方法3:修改项目文件恢复设计器支持

在SDK风格的.csproj文件中添加以下配置,让Visual Studio识别WebForms项目类型,恢复自动生成.designer.cs的功能:

<PropertyGroup>
  <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
  <WebApplication>true</WebApplication>
</PropertyGroup>

解释:{349c5851-65df-11da-9384-00065b846f21}是WebForms项目的类型GUID,添加后Visual Studio会启用对应设计器支持。


内容的提问来源于stack exchange,提问作者ScottishTapWater

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 16:32:40