C# Use HttpContext.Current.Request Cannot get QueryString e when requested on mobile browser

Viewed 5300

This is my public method(APIUtility)

public static string GetRequestData(string key, string defaultVal)
{
    if (HttpContext.Current != null && HttpContext.Current.Request != null)
    {
        return HttpContext.Current.Request[key] == null || HttpContext.Current.Request[key].Trim() == "" ? defaultVal : HttpContext.Current.Request[key].Trim();
    }
    else
    {
        return defaultVal;
    }
}

Have the html used javascript location.href(aa.json?key=value&key1=value1....) go to url my class funtion

In the Function aa used string getUrlValue = APIUtility.GetRequestData(key name) get Querystring.

The user uses the phone browser (OppoBrowser or safari..) to jump to the server function through my html page Unable to get querystring is empty, but if using a computer is normal.

Hope you can understand what I want to express.

1 Answers

Try System.Web.HttpContext.Current.Request.QueryString

public static string GetRequestData( string key, string defaultVal ) {
    try {
        var ctx = System.Web.HttpContext.Current;
        var value = ctx.Request.QueryString[key];
        return string.IsNullOrEmpty(value) ? defaultVal : value;
    } catch {
        return defaultVal;
    }
}

Learn more

Related