【问题标题】:Hosting a webform page inside a web api. CS0103 control does not exist in the current context在 web api 中托管 webform 页面。 CS0103 控件在当前上下文中不存在
【发布时间】:2019-05-10 19:59:50
【问题描述】:

我需要将一个 ASP.NET Web 表单页面添加到我的 ASP.NET API 项目中。这是一个遗留应用程序,我还没有时间移植到 API 和 Angular。旧页面包含多个控件,包括文本框、下拉列表和 GridView 控件。 Web 表单的所有代码逻辑都包含在代码隐藏页面中。

我遇到的问题是,当我尝试重建项目时,无法识别旧版 Web 表单中的控件。我收到以下错误

Error CS0103 The name '<name of web control>' does not exist in the current context MyWebApp C:\source_code\MyWebApp\api\services\LegacyWebForm.aspx.cs

对于 ASPX 页面上的所有服务器端控件,我都遇到了这些错误之一。

我很确定我可以在 API 应用程序中运行这个旧版 Web 表单,我需要做的就是添加...

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

我的代码隐藏文件LegacyWebForm.aspx.cs继承自System.Web.UI.Page,ASPX页面继承页面类

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LegacyWebForm.aspx.cs" Inherits="MyWebApp.api.services.LegacyWebForm" %>

代码隐藏类名

namespace MyWebApp.api.services
{
    public partial class LegacyWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
...

【问题讨论】:

  • 应该是有效的,你不是缺少runats吗?
  • 我在包含 &lt;form&gt; 元素和所有控件上确实有 runat="server"
  • 你的.aspx.designer.cs注册了吗? (protected global::System.Web.UI.WebControls.TextBox someTextBox;,那个文件有正确的命名空间吗?
  • Designer 它将在文件层次结构中与文件.aspx.cs 代码后面的.aspx.cs 代码相邻regenerating the designer
  • @UK_Dev - 做到了!您提供给Designer 的链接包含重新生成 .aspx.designer.cs 文件的所有说明。之后它编译并运行。非常感谢!仅供参考 - David-Dietrich 的答案包含我遵循的步骤。

标签: asp.net asp.net-web-api webforms


【解决方案1】:

您似乎缺少一个 designer 文件。

.aspx.designer.cs

This 突出了设计师的用法。

您可以regenerate 设计器文件。

如果在您尝试编译时 desiger 文件仍然抛出丢失的引用,您可能需要清理项目并重新构建它。

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 2022-11-17
    • 2017-03-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    相关资源
    最近更新 更多