如何确保对可能更改的页面的引用面向未来
简单的例子..
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您正在做的事情来说可能有点过分了,但是请查看 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.