使用页面类型获取 aspx 页面的 url
我正在使用一个网络应用程序项目。
我的网络根目录中有一个名为 Users 的文件夹,在该文件夹中我有一个名为 UserList.aspx 的页面,
我想要做的是输入 Response.Redirect(Users.UserList.URL)
我认为我可以做的是创建一个扩展 Page 的类,并添加一个名为 URL 的静态属性,该属性调用 MethodInfo.GetCurrentMethod().ReflectedType (我认为这有效尚未测试),然后将其转换为 Users.UserList -> ~/Users/UserList.aspx
据我所知,这种方法的问题是我需要遍历每个页面并使其扩展基类,并且它不适用于任何包含“-”字符的页面。
优点是,如果页面移动,则不会出现任何损坏的链接(当存在具有错误命名空间的页面时,Resharper 会给出提示)。
此外,每个采用查询字符串参数的单独页面都可以有一个静态方法,这样如果我想添加/删除参数,我可以看到什么使用了这些参数等。 另外,如果我想调用该页面,我不必检查参数的名称,例如 UserId userId、Id 或 id。所以看起来像 Users.ViewUser.GetUrl(1) -> ~/Users/ViewUser.aspx?UserId=1
所以问题是:有更好的方法吗?或者这在原则上是一个坏主意?
I'm using a web application project.
I have a folder in my web root called Users and in the folder I have a page called UserList.aspx
What I want to be able to do is type in Response.Redirect(Users.UserList.URL)
What I reckon I can probably do is create a class that extends Page and add a static property called URL that calls MethodInfo.GetCurrentMethod().ReflectedType (I think this works haven't tested) and then have that convert Users.UserList -> ~/Users/UserList.aspx
The problems with this method that I know of are one I need to go through every page and make it extend the base class and it doesn't work with any pages that contain a '-' character.
The advantages are that if pages are moved around then there aren't any broken links (Resharper gives out when there is a Page with the wrong namespace).
Also then every individual page that takes query string params could have a static method so that if I want to add/remove params I can see what uses those params etc.
Also if I want to call that page I don't have to check the name of the params e.g. UserId userId, Id or id. So that would look something like Users.ViewUser.GetUrl(1) -> ~/Users/ViewUser.aspx?UserId=1
So the question is: Is there a better way of doing this? Or is this a bad idea in principal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为基本 Page 类创建一个扩展方法来执行您的想法。这将避免必须返回并修改所有页面的基类。
You could just create an extension method for the base Page class that does what you are thinking. That would avoid having to go back and modify the base class for all your pages.
有一个更好的方法。创建一个了解路径的交通警察。然后,如果路径发生变化,您的数据模型发生变化或其他内容,您只需更改该位置即可。另外,您可以读取配置文件并在运行时进行更改。
因此,您的调用如下所示:
Repose.Redirect(TrafficCop["Users.UserList"].URL)
或其他方式(如果您不喜欢该语法)。
There is a better way. Create a traffic cop that knows about paths. Then if paths change, your data model changes or other stuff you just change that one place. Plus you could have read from a config file and make changes at run time.
Thus your call looks like this:
Repose.Redirect(TrafficCop["Users.UserList"].URL)
or some other way if you don't like the syntax.
MethodInfo.GetCurrentMethod().ReflectedType 不起作用,因此我想出了另一种使用泛型来执行此操作的方法。
它不是 Users.ViewUser.GetUrl() 或 Users.ViewUser.URL,而是 GetUrl()
对于带有参数的页面,它仍然是 Users.ViewUser.GetUrl(1),这并不理想,因为它们应该具有相同的调用方式,但我猜比字符串更好。
为了以防万一,我们暂时将这个问题悬而未决。
编辑:我想我实际上只会创建另一个名为 GetUrl(String getQuery) 的方法,因为如果我有两个相同类型的参数,它就不能很好地工作。
进一步编辑:我找到了如何准确地做我想做的事情。
创建了一个名为 BasePage:Page 的类,其中 T : Page
上面是静态方法redirect和geturl
每个页面都继承自基页,如下所示:MyPage:BasePage
任何页面都可以使用命令 MyPage.Redirect() 重定向到该页面;
The MethodInfo.GetCurrentMethod().ReflectedType doesn't work so I came up with another method of doing this using generics.
Instead of Users.ViewUser.GetUrl() or Users.ViewUser.URL it's GetUrl()
For a page with parameters it's still Users.ViewUser.GetUrl(1), it isn't ideal because they should both have the same way of being called but better than strings I guess.
Going to leave the question open for a while just in case.
edidt: I think I will actually just create another method called GetUrl(String getQuery) because if I have two parameters that are of the same type it doesn't work very well.
further edit: I found out how to do exactly what I want to do.
created a class called BasePage:Page where T : Page
on that are the static methods redirect and geturl
each page inherits from the base page as follows: MyPage:BasePage
Any page can redirect to that page by using the command MyPage.Redirect();