0

I have a page http://localhost:65248/ZeroLabs/msg_preview.aspx.

I want to get http://localhost:65248/ZeroLabs part.

My code runs in this page Page_Load event.

I didn't find anything good in Request.Url.

enter image description here

Currently I use this:

//http + :// + localhost:65248 + /ZeroLabs
string omg_wtf = Request.Url.Scheme + "://" + Request.Url.Authority + System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;

I feel this isn't a way how I supposed to get what I want. Any less ugly way, please?

Kosmo零
  • 4,001
  • 9
  • 45
  • 88
  • 1
    Well, is `Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("/"))` any less "ugly"? Please define "ugly" in an objective, non-opinionated way. – Heretic Monkey May 27 '21 at 13:48
  • @HereticMonkey - Well... I hoped for some single property... but your way is less ugly too... Or maybe no if we have something like that `?asd=qwe/sdf`... Or maybe yes if we can't have something like that in AbsoluteUri... – Kosmo零 May 27 '21 at 13:52
  • Curious why you want it. Generally you can just use tilde to construct paths to resources. There's a bunch of ways to get the full path though [here](https://stackoverflow.com/questions/5853294/how-do-i-get-the-site-root-url) – Nikki9696 May 27 '21 at 14:08
  • @Nikki9696 - I use it to concatenate with other page names in code. For example I build an list of *.aspx pages stored in physical path (`string[] paths = Directory.GetFiles(path, "*.aspx")`) corresponding to `http://localhost:65248/ZeroLabs` and give it to user, so he can visit them. It's like `http://localhost:65248/ZeroLabs` + `/` + paths[i]. – Kosmo零 May 27 '21 at 14:13

1 Answers1

1

I am not 100% sure, as it's a long time using web pages with Asp.Net web forms. But If I recall correctly, you just need to use ~ for getting the virtual path of you web application/website.

So ~/ZeroLabs should work for you. Just give it a try.

Also see this link on Stackoverflow. It might be useful.

Thanks

vivek
  • 1,595
  • 2
  • 18
  • 35
  • Thanks. I found `HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath;` there. Still not a single property, yet this this better than my junk code. – Kosmo零 May 28 '21 at 06:40