none
Parse String URL RRS feed

  • Question

  • I think I've seen this before but I can't find any examples. I need to parse a url to pull out a piece of it and put it into a variable. The URL will contain "site=". I need to grab that part of the url. Here is what I've built so far for my function:

    private string BuildSite() { string str = string.Empty; string url = HttpContext.Current.Request.Url.AbsoluteUri;

    //parse value return str; }

    I think first I need to use find; to search for "site=" or should I split on that then split again on "/" to break the string to pull it out? Not sure how to approach this issue. I hope their is a built in function already 

    Thursday, January 10, 2019 3:15 PM

Answers

  • Use Uri. If you are looking for site= then I suspect you're looking for the query string parameter site. 

    var str = @"https://www.tempuri.org/myapp?key1=value1&site=siteName";
    
    var uri = new Uri(str, UriKind.Absolute);
    
    //Using LINQ here and lowercasing parameter because case shouldn't matter - different ways to do this
    var parameters = from u in uri.Query.Split('&')
                        let parts = u.Split('=')
                        select new { Name = parts[0].ToLower(), Value = parts[1] };
    
    var site = parameters.FirstOrDefault(f => f.Name == "site");

    This would be a good extension method. But I notice that you're actually in a web app (please post questions related to web apps in the ASP.NET forums). In this specific case you can just use the HttpRequest.QueryString property instead. It already breaks out the parameters into a dictionary. If the value may come from different places besides query string then use the indexer instead.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by old_School Thursday, January 10, 2019 5:05 PM
    Thursday, January 10, 2019 3:44 PM
    Moderator

All replies

  • Use Uri. If you are looking for site= then I suspect you're looking for the query string parameter site. 

    var str = @"https://www.tempuri.org/myapp?key1=value1&site=siteName";
    
    var uri = new Uri(str, UriKind.Absolute);
    
    //Using LINQ here and lowercasing parameter because case shouldn't matter - different ways to do this
    var parameters = from u in uri.Query.Split('&')
                        let parts = u.Split('=')
                        select new { Name = parts[0].ToLower(), Value = parts[1] };
    
    var site = parameters.FirstOrDefault(f => f.Name == "site");

    This would be a good extension method. But I notice that you're actually in a web app (please post questions related to web apps in the ASP.NET forums). In this specific case you can just use the HttpRequest.QueryString property instead. It already breaks out the parameters into a dictionary. If the value may come from different places besides query string then use the indexer instead.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by old_School Thursday, January 10, 2019 5:05 PM
    Thursday, January 10, 2019 3:44 PM
    Moderator
  • Thanks I'll give it a shot. Its actually a custom extension/dll. 

    Edit:

    Worked thank you so much 

    • Edited by old_School Thursday, January 10, 2019 5:06 PM
    Thursday, January 10, 2019 4:41 PM