ASP.NET 如何阻止路由应用程序中的底层 Web 窗体被直接访问?
想象一个带有路由的 Web 表单应用程序。 干净的页面名称,例如:
可能有一个 URL 底层的:
http://www.mywebsite.com/page.aspx?id=3
如果用户输入 http://www.mywebsiter.com/page.aspx?id =3 进入浏览器,我需要重定向到 http://www.mywebsite.com/home< /a>
这可能吗? 我无法找到一种方法来执行此操作,因为路由引擎不是针对物理页面执行的,并且在 page.aspx Page_Load 方法中我无法知道 URL 是直接输入的还是路由的结果。
Imagine a Web Forms application with routing.
A clean page name like:
Might have an underlying of URL of:
http://www.mywebsite.com/page.aspx?id=3
If a user enters http://www.mywebsiter.com/page.aspx?id=3 into a browser, I need to redirect to http://www.mywebsite.com/home
Is this possible to do?
I can't work out a way to do this as the routing engine is not executed for a physical page and in the page.aspx Page_Load method I have no way of knowing whether the URL was entered directly or was the result of a route.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Page.RouteData.Values
集合来检测页面是否是由于路由而不是直接 URL 加载的。这可以在 Page_Load() 中完成。如果存在路线数据值(您可能会检查您知道应该存在的值),那么它们就很好。如果没有路由数据值,则页面已“直接”加载,您应该重定向它们。
You can use the
Page.RouteData.Values
collection to detect if the page is being loaded due to routing, rather than a direct URL. That can be done in Page_Load().If there are route data values (you would likely check for values that you would know should exist), then they are fine. If there are no route data values, the page has loaded 'directly', and you should redirect them.
查看 IIS URL 重写模块。
您还可以查看诸如禁用文件路由之类的内容(
RouteTable.Routes.RouteExistingFiles = false;
) - 但这可能很危险!Check out the IIS URL rewrite module.
You could also look at things like disabling routing for files (
RouteTable.Routes.RouteExistingFiles = false;
) - that could be dangerous though!