Get url parameters from a string in .NET

前端 未结 13 1793
一个人的身影
一个人的身影 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 11:53

    Or if you don't know the URL (so as to avoid hardcoding, use the AbsoluteUri

    Example ...

            //get the full URL
            Uri myUri = new Uri(Request.Url.AbsoluteUri);
            //get any parameters
            string strStatus = HttpUtility.ParseQueryString(myUri.Query).Get("status");
            string strMsg = HttpUtility.ParseQueryString(myUri.Query).Get("message");
            switch (strStatus.ToUpper())
            {
                case "OK":
                    webMessageBox.Show("EMAILS SENT!");
                    break;
                case "ER":
                    webMessageBox.Show("EMAILS SENT, BUT ... " + strMsg);
                    break;
            }
    

提交回复
热议问题