C# 使用 HttpContext.Current.Request 在移动浏览器上请求时无法获取 QueryString e

余致賢*_*余致賢 5 c# asp.net query-string

这是我的公共方法(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;
    }
}
Run Code Online (Sandbox Code Playgroud)

让 html 使用 javascriptlocation.href(aa.json?key=value&key1=value1....)去 url 我的类功能

在函数 aa 中使用了string getUrlValue = APIUtility.GetRequestData(key name)get Querystring。

用户使用手机浏览器(OppoBrowser或safari..)通过我的html页面跳转到服务器功能Unable to get querystring为空,但如果使用电脑正常。

希望你能明白我想表达的意思。

RKT*_*XYN 4

尝试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;
    }
}
Run Code Online (Sandbox Code Playgroud)

了解更多