Get url parameters from a string in .NET

前端 未结 13 1661
一个人的身影
一个人的身影 2020-11-22 11:38

I\'ve got a string in .NET which is actually a url. I want an easy way to get the value from a particular parameter.

Normally, I\'d just use Request.Params[

相关标签:
13条回答
  • 2020-11-22 12:09

    Looks like you should loop over the values of myUri.Query and parse it from there.

     string desiredValue;
     foreach(string item in myUri.Query.Split('&'))
     {
         string[] parts = item.Replace("?", "").Split('=');
         if(parts[0] == "desiredKey")
         {
             desiredValue = parts[1];
             break;
         }
     }
    

    I wouldn't use this code without testing it on a bunch of malformed URLs however. It might break on some/all of these:

    • hello.html?
    • hello.html?valuelesskey
    • hello.html?key=value=hi
    • hello.html?hi=value?&b=c
    • etc
    0 讨论(0)
提交回复
热议问题