2012-07-10 61 views
0

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

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"; 
} 

其他人如何处理这个问题?也许我过于复杂了,尽管我正在处理一百个URL,并且有很多页面引用每个URL。

回答

0

考虑使用资源文件。通过这种方式,您可以保持对页面的一致引用,但是如果情况需要,它可以在代码中轻松维护,并且易于热固定后部署。

0

你可以尝试编写一些T4模板来生成你的UrlManager文件类。像T4MVC。在这里寻找WebForms示例T4Mvc web forms