如何确保对可能更改的页面的引用面向未来

发布于 2024-09-30 16:55:50 字数 207 浏览 1 评论 0原文

简单的例子..

Response.Redirect("/store/packages.aspx");

如果可能的话,我想直接从 web.sitemap 文件引用“/store/packages.aspx”。这样,如果有一天我重命名或移动文件 packages.aspx,我不必找到我在代码中硬编码 URL 的每个实例。

实现这一目标的最佳方法是什么?

Simple example..

Response.Redirect("/store/packages.aspx");

I'd like to reference "/store/packages.aspx" directly from the web.sitemap file, if at all possible. This way if some day down the line I rename or move the file packages.aspx, I don't have to find every instance of where I hard-coded the URL in my code.

What is the best method for achieving this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛溺水了 2024-10-07 16:55:51

对于您正在做的事情来说可能有点过分了,但是请查看 T4MVC。简而言之,它从项目中的文件生成类。然后,您可以引用生成的类的成员,而不是对 URL 进行硬编码,如果名称发生更改并且您的引用尚未更新,则会出现编译错误。它是专门为 MVC 构建的,但可以修改模板以满足您的需求。

正如 RPM1984 所提到的,一个更简单的解决方案是维护一组 const 字段并在需要路径的地方引用这些字段。

It may be overkill for what you're doing, but check out T4MVC. The short version is that it generates classes from files in your project. You then reference the generated classes' members instead of hard-coding the URL, and get a compile error if the names change and your reference hasn't been updated. It's built specifically for MVC, but the templates could probably be modified to meet your needs.

As mentioned by RPM1984, a simpler solution is to maintain a set of const fields and reference those wherever you need paths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文