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

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

这是我的public方法(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;
    }
}

让 html 使用 javascript location.href(aa.json?key=value&key1=value1....) 转到 url 我的 class 功能

在函数 aa 中使用 string getUrlValue = APIUtility.GetRequestData(key name) 获取查询字符串。

用户使用phone浏览器(OppoBrowser或safari..)通过我的html页面跳转到服务器功能 无法获取querystring为空,但是如果使用电脑是正常的。

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

尝试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