在WebForms中管理URL

时间:2012-07-10 14:15:08

标签: asp.net webforms

我有一个大型的Webforms应用程序。在整个应用程序的许多地方,我们在代码后面设置了超链接的导航URL。硬编码字符串文字似乎是一个坏主意。

hlVideos.NavigateUrl = "/path/to/some/page.aspx";

这似乎也不是一个好主意,因为它可能要求我在每个需要它的页面上都有一个常量字符串:

private const string PathToSomePage = "/path/to/some/page.aspx";

hlVideos.NavigateUrl = PathToSomePage;    

我已经考虑过一个单独的类,其中包含一堆可以访问的const字符串。这似乎是一个开放/封闭的原则违规,要求我在每次添加新页面时添加另一个常量。

public class UrlManager
{
    public const string PathToSomePage = "/path/to/some/page.aspx";
    public const string PathToSomeOtherPage = "/path/to/some/other/page.aspx";
    public const string PathToYetAnotherPage = "/path/to/yet/another/page.aspx";
}

其他人如何处理这个问题?也许我过于复杂了,虽然我正在处理一百个网址,有很多页面引用每个网址。

2 个答案:

答案 0 :(得分:0)

考虑使用资源文件。这样,您就可以保持对页面的一致引用,但如果情况需要,可以在代码中轻松维护,并在部署后轻松进行热修复。

答案 1 :(得分:0)

您可以尝试编写一些T4模板来生成UrlManager文件类。像T4MVC这样的东西。在这里查看WebForms示例T4Mvc web forms