Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 115355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:00:22+00:00 2026-05-11T03:00:22+00:00

Given an absolute URI/URL, I want to get a URI/URL which doesn’t contain the

  • 0

Given an absolute URI/URL, I want to get a URI/URL which doesn’t contain the leaf portion. For example: given http://foo.com/bar/baz.html, I should get http://foo.com/bar/.

The code which I could come up with seems a bit lengthy, so I’m wondering if there is a better way.

static string GetParentUriString(Uri uri)     {                     StringBuilder parentName = new StringBuilder();          // Append the scheme: http, ftp etc.         parentName.Append(uri.Scheme);                      // Appned the '://' after the http, ftp etc.         parentName.Append('://');          // Append the host name www.foo.com         parentName.Append(uri.Host);          // Append each segment except the last one. The last one is the         // leaf and we will ignore it.         for (int i = 0; i < uri.Segments.Length - 1; i++)         {             parentName.Append(uri.Segments[i]);         }         return parentName.ToString();     } 

One would use the function something like this:

  static void Main(string[] args)     {                     Uri uri = new Uri('http://foo.com/bar/baz.html');         // Should return http://foo.com/bar/         string parentName = GetParentUriString(uri);                             } 

Thanks, Rohit

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:00:22+00:00Added an answer on May 11, 2026 at 3:00 am

    This is the shortest I can come up with:

    static string GetParentUriString(Uri uri) {     return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length); } 

    If you want to use the Last() method, you will have to include System.Linq.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be simple. I want to get an absolute Uri given the controller,
What's the easiest way to get the URL (relative or absolute) to a Route
Given a string containing an absolute URL with a querystring parameter, what is the
how to get the absolute path of given object id using java dfc in
Given an absolute url of a page, and a relative link found within that
Given absolute or relative position (top & left) is there any way to get
Given that the web application doesn't have su privileges, I'd like to execute a
Given a certain date, I want to set the value of a cell with
Given is an array of 320 elements (int16), which represent an audio signal (16-bit
I need to combine two URL's that both contain .Path information. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.