How can I parse HTTP urls in C#?

前端 未结 3 1624
小蘑菇
小蘑菇 2020-12-01 16:50

My requirement is to parse Http Urls and call functions accordingly. In my current implementation, I am using nested if-else statement which i think is not an optimized way.

3条回答
  •  渐次进展
    2020-12-01 17:23

    This might come as a bit of a late answer but I found myself recently trying to parse some URLs and I went along using a combination of Uri and System.Web.HttpUtility as seen here, my URLs were like http://one-domain.com/some/segments/{param1}?param2=x.... so this is what I did:

    var uri = new Uri(myUrl);
    string param1 = uri.Segments.Last();
    
    var parameters = HttpUtility.ParseQueryString(uri.Query);
    string param2 = parameters["param2"];
    

    note that in both cases you'll be working with strings, and be specially weary when working with segments.

提交回复
热议问题