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?

4
  • 1
    Well, is Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf("/")) any less "ugly"? Please define "ugly" in an objective, non-opinionated way. Commented May 27, 2021 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... Commented May 27, 2021 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 Commented May 27, 2021 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]. Commented May 27, 2021 at 14:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.