我有一个项目,我的任务是在一组旧 C#/VB Web 项目中将硬编码域引用从一个域更改为另一个域。我想尽可能多地参数化域,而不是仅仅用不同的硬编码值替换。问题是,大约 30 个不同的解决方案中有超过 800 个这样的引用,因此在每个代码隐藏中创建要绑定的变量将花费很长时间。
我已将新域添加到 web.config 文件的 appSettings 部分,并且这有效:
<asp:HyperLink Text="Link" NavigateUrl="<%$appSettings:DomainX %>" runat="server" />
但我需要能够执行以下操作:
<asp:HyperLink Text="Link" NavigateUrl="<%$appSettings:DomainX %>/newPage.aspx" runat="server" />
但是当我添加“/newPage.aspx”时,页面不会编译不再了。我真的不在乎这是使用 asp:HyperLink 标签还是只是一个标签来完成。
关于如何实现这一目标有什么想法吗?
谢谢。
I've got a project where I'm tasked with changing hard-coded domain references from one domain to another in a group of legacy C#/VB web projects. I want to parameterize the domains as much as possible instead of just replacing with a different hard-coded value. The problem is that there are over 800 of these references in about 30 different solutions, so creating variables in each code-behind to bind to would take forever.
I have added the new domains to the appSettings section of the web.config file, and this works:
<asp:HyperLink Text="Link" NavigateUrl="<%$appSettings:DomainX %>" runat="server" />
But I need to be able to do something like this:
<asp:HyperLink Text="Link" NavigateUrl="<%$appSettings:DomainX %>/newPage.aspx" runat="server" />
But when I add the "/newPage.aspx" the page doesn't compile anymore. I don't really care if this is done with an asp:HyperLink tag or just a tag.
Any ideas on how I can accomplish this?
Thanks.
发布评论
评论(3)
我认为你有两个选择。如果您没有对
HyperLink
服务器端执行任何操作,最简单的方法是仅使用普通的旧锚标记:或者,您可以在
NavigateUrl
中设置NavigateUrl
>Page_Load,因为 <%= 在HyperLink
服务器标记中无法正常工作:您还可以查看是否可以进行自定义绑定,例如
$myBinding:DomainX
,但我不知道这是否可能在我的脑海中出现(我认为是这样)。编辑
该
$appSettings:DomainX
代码称为 ASP.NET 表达式,您可以创建自定义表达式。 Phil Haack 的这篇帖子介绍了如何设置它们,以防万一您有兴趣。I think you have two options. The easiest is to just use a plain old anchor tag, if you're not doing anything with the
HyperLink
server side:Alternatively, you could set the
NavigateUrl
in thePage_Load
, since <%= will not work properly within theHyperLink
server tag:You could also see if you can make a custom binding, something like
$myBinding:DomainX
, but I don't know if that's possible off the top of my head (I would assume it is though).EDIT
That
$appSettings:DomainX
code is called an ASP.NET Expression, and can you can create custom expressions. This post from Phil Haack covers how to set them up, in case you are interested.类似这样的事情怎么样:
How about something along the lines of:
我会选择两种不同的方法之一,这样您就无需更改
.aspx
本身中的NavigateUrl
了。一种选择是从 HyperLink 类继承并重写 更改为
NavigateUrl
属性,在 getter 方法中添加ConfigurationManager.AppSettings["DomainX"]
。有了这个,只需将第二个选项是向每个页面添加小功能(可以在一个页面中)共享位置并且只是从每个页面调用),它将迭代所有超链接控件并动态添加域。要查找某种类型的所有控件,您可以使用 。
I would go with one of two different approaches that would save you the need to change the
NavigateUrl
in the.aspx
itself.One option is to inherit from HyperLink class and overriding the
NavigateUrl
property, adding theConfigurationManager.AppSettings["DomainX"]
in the getter method. Having this, just change<asp:HyperLink ...
to<UC:MyLink ...
Second option is adding small function to each page (can be in one shared place and just called from each page) that will iterate over all the hyperlinks controls and dynamically add the domain. To find all controls of certain type you can use such code for example.