如何根据域/主机调整asp.net URL路由?
根据域/子域/主机调整在 global.asax Application_Start 事件中创建的路由表的路径目标的最佳方法是什么?以下内容在 IIS6 中有效,但在 IIS7 中,请求对象与 Application_Start 事件分离,因此不再有效:
Dim strHost As String = Context.Request.Url.Host
Dim strDir As String = ""
If strHost.Contains("domain1.com") Then
strDir = "area1/"
Else
strDir = "area2/"
End If
routes.MapPageRoute("Search", "Search", "~/" & strDir & "search.aspx")
What's the best way to adjust the path destination for a routing table created in the global.asax Application_Start event based on the domain/sub domain/host? The following worked in IIS6, but with IIS7 the request object is decoupled from the Application_Start event and therefore does not work anymore:
Dim strHost As String = Context.Request.Url.Host
Dim strDir As String = ""
If strHost.Contains("domain1.com") Then
strDir = "area1/"
Else
strDir = "area2/"
End If
routes.MapPageRoute("Search", "Search", "~/" & strDir & "search.aspx")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我似乎已经解决了我自己的问题。您无法使用 IIS7.0 访问 Application_Start 处的 Request 对象,但您可以在自定义路由约束中使用它。我是这样做的。
定义自定义路由约束:
然后定义路由:
该技术也可用于基于子域应用不同的路由。
非常感谢 Steven Wather 的 asp.net mvc 路由 帖子为我指明了正确的方向(即使它是针对 mvc 而不是 Web 表单)。
I seem to have solved my own problem. You can't access the Request object at Application_Start with IIS7.0, though you can use it in a custom route constraint. Here's how I did it.
Define the custom route constraint:
Then define the route:
This technique can also be used for applying different routing based on the sub-domain as well.
Big thanks to Steven Wather's asp.net mvc routing post for pointing me in the right direction (even though it was for mvc and not web forms).
您可以从 web.config 中读取此设置吗? <-我的建议。
这篇文章有帮助吗?
http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx< /a>
Is this a setting you could read from web.config instead? <- my recommendation.
Does this post help?
http://mvolo.com/blogs/serverside/archive/2007/11/10/Integrated-mode-Request-is-not-available-in-this-context-in-Application_5F00_Start.aspx