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

2019-08-28 05:06发布

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!

3条回答
做个烂人
2楼-- · 2019-08-28 05:27

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.

查看更多
Emotional °昔
3楼-- · 2019-08-28 05:44

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.

查看更多
家丑人穷心不美
4楼-- · 2019-08-28 05:51

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().

查看更多
登录 后发表回答