Asp.NET / VB.NET: Getting the path from the URL / URI?

ε祈祈猫儿з 提交于 2019-12-13 04:15:59

问题


Say I have a project that I am deploying at

www.foo.com/path1/default.aspx

and

www.foo.com/path2/default.aspx

What would be the most reliable way to know if I was in the folder "path1", or "path2"? Can I grab that directly, or do I need to split() somehow on the Request.Url.AbsolutePath, or... ?

I just want to change colors, etc. based on which folder the user is in.

Thanks for any assistance!


回答1:


If you want to code that logic directly into the page, then yeah, I'd go with split() on Request.Url.AbsolutePath.

That said, I'd consider storing this kind of setting in the AppSettings section of web.config. That way if you decide to change the color in path2, you just need to edit the web.config for path2. If you need to add a new path, just deploy there and edit the web.config as appropriate.




回答2:


Yeah use Request.Url.AbsolutePath.

I do it to create Breadcrumbs, using Split to split the URL, then in your case I suggest to use Switch statement to change color based on the case of the Switch statement

Here is a great article about Paths in ASP.




回答3:


Check out the MSDN docs on System.IO.Path. It contains a number of useful functions for dealing with path names. You can get GetDirectoryName() or GetFullPath() or GetFileName() or GetFileNameWithoutExtension().



来源:https://stackoverflow.com/questions/830755/asp-net-vb-net-getting-the-path-from-the-url-uri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!