将自定义用户控件添加到已发布的 Web 项目
我开始怀疑这是否可能。看起来就是这么简单。
我有一个已发布的网络项目。我想向该发布的网站添加一些 .ascx 文件(带有 .cs 和 Designer.cs 文件)。这些是简单的自定义用户控件,可以访问原始应用程序中已经包含的方法。
问题?是否可以将它们放在已发布的 Web 项目中而不构建整个解决方案?如果不是为什么?
当我将这些文件放入并运行我的应用程序时,我收到错误: “解析错误:无法加载类型‘我的自定义控件命名空间的名称’”。
没有太多代码可以展示,所以这就是我所拥有的。
Default.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPages/TwoColumn.master" AutoEventWireup="true"
Inherits="ApplicationName.Web.Default" CodeBehind="Default.aspx.cs" %>
<%@ Register TagPrefix="uc1" TagName="CustomControl" Src="~/Controls/Custom/CustomControl.ascx" %>
<asp:Content ID="content" contentplaceholder="cph" runat="Server">
<uc1:CustomControl ID="cc1" runat="server" CustomProperty="Hello World" />
</asp:Content>
CustomControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustomControl.ascx.cs"
Inherits="ApplicationName.Web.Controls.Custom.CustomControl" %>
<asp:PlaceHolder ID="ph1" runat="server></asp:PlaceHolder>
CustomControl.ascx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ApplicationName.Web.Controls.Custom
{
public partial class CustomControl : System.Web.UI.UserControl
{
///My logic
}
}
再次看起来如此简单。我缺少什么?或者这是不可能的?谢谢。
更新: 我明白了这一点。上述场景是可能的。正如错误消息所示,问题不在于名称空间。相反,它是代码隐藏声明。发布应用程序时会编译任何类型文件的代码隐藏文件。我仍然很困惑为什么当您浏览 Web 目录时它似乎是可编辑的,我认为它会存储在 .dll 文件或其他文件中。也许有人可以阐明这一点。
无论如何,用代码文件替换代码隐藏可以纠正问题,因为代码文件未编译,因此在应用程序运行时可读。
I am beginning to wonder if this is even possible. It just seems so simple.
I have a published web project. I want to add some .ascx files (with .cs & designer.cs files) to that published web site. These are simple custom user controls that access methods already part of the original application.
Question? Is it possible to just drop these in the published web project without building the entire solution? If not why?
When I drop these files in and run my application I get the error:
"Parse Error: Could not load type 'the name of my custom controls namespace'".
There is not a lot of code to show so this is all I have.
Default.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPages/TwoColumn.master" AutoEventWireup="true"
Inherits="ApplicationName.Web.Default" CodeBehind="Default.aspx.cs" %>
<%@ Register TagPrefix="uc1" TagName="CustomControl" Src="~/Controls/Custom/CustomControl.ascx" %>
<asp:Content ID="content" contentplaceholder="cph" runat="Server">
<uc1:CustomControl ID="cc1" runat="server" CustomProperty="Hello World" />
</asp:Content>
CustomControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CustomControl.ascx.cs"
Inherits="ApplicationName.Web.Controls.Custom.CustomControl" %>
<asp:PlaceHolder ID="ph1" runat="server></asp:PlaceHolder>
CustomControl.ascx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ApplicationName.Web.Controls.Custom
{
public partial class CustomControl : System.Web.UI.UserControl
{
///My logic
}
}
Again it seems so easy. What am I missing? Or is this not possible? Thanks.
UPDATE:
I figured this out. The above scenario is possible. The problem is not with the name-space as the error message suggests. Rather it is the code-behind declaration. Code-behind files for any type of file are compiled when the application is published. I am still confused as to why it appears to be editable when you browse through a web directory, I would think it would be stored in a .dll file or something. Maybe someone can shed some light on this.
Anyways, replacing code-behind with code-file rectifies the problem as code-files are not compiled and are therefore readable at application run-time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是可能的,但您仍然需要编译用户控件并将该 dll 放入应用程序的正确 bin 目录中。这通常是您描述的类型加载错误的原因。
It is possible but you still have to compile your user control and drop that dll into the proper bin directory for your app. That's usually the cause of the type loading error you described.
这种方法可能很草率,您基本上要么
您是否考虑过一种更简洁的方法,只创建一个继承自 System.Web 的类.Ui.Control 然后将其添加到 .common 项目中?然后将其放入正确的项目中?您的方法的问题在于预编译和部署,您最终可能会尝试将两个用户控件放入同一个文件夹中,这将破坏构建......
替代方法(和微软的方式)将是这样的......
< strong>代码 - 编写自定义控件
页面引用 - 如何在您的 UI 项目中引用它
Web.config - 将此控件导入到您的所有项目中所需的内容用户界面页面
This approach could be sloppy, you are basically either
Have you thought about a cleaner approach and just creating a class that inherits from System.Web.Ui.Control and then adding this in a .common project? Then pulling this into the corrct project? The problem with your approach is on precompilation and deployment you could end up trying to put two user controls into the same folder which will break the build....
The alternate approach (and the microsoft way) would be like this...
The code - write a custom control
The page reference - how to reference it in your UI project
Web.config - what you need to import this control into all of your UI pages