【问题标题】:Empty Request.Form using $http angular to post空 Request.Form 使用 $http Angular 发布
【发布时间】:2019-12-05 20:10:41
【问题描述】:

无法从 C# 中的请求中获取我的数据。我所拥有的是有角度的 $http 发布到代理 api。问题是 Request.Form 是空的; Request.InputStream 也是如此。

角度:

$http({
                method: "POST",
                url: '/apiproxy/projects/' + data.Project.Id + '/recruiting-groups',
                data: angular.toJson(group, false)
            });

C#:

 public ActionResult Index(string pathInfo)
        {
            var url = Settings.GetValue<string>("QualService") + "/" + pathInfo + "?" + Request.QueryString;


            //Get stuff from the back end
            using (var client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                client.Headers[HttpRequestHeader.Cookie] = Request.Headers["Cookie"];
                client.Headers[HttpRequestHeader.Authorization] = "Basic " +
                                                                  Convert.ToBase64String(
                                                                      Encoding.UTF8.GetBytes(
                                                                          "x:{0}".Fmt(UserSession.ApiKey)));


                try
                {
                    var responseBytes = Request.HttpMethod == "POST" ? client.UploadValues(url, Request.Form) : client.DownloadData(url);

                    var result = new ContentResult();
                    result.Content = Encoding.UTF8.GetString(responseBytes);
                    result.ContentEncoding = Encoding.UTF8;
                    result.ContentType = "application/json";
                    return result;
                }
                catch(Exception e)
                {
                    Logger.Error("Error while proxying to the API:  ", e);
                }
            }

            return Json(false);
        }

当我在 chrome 中查看网络选项卡时,我可以看到正在传递的数据:

尽管 Request.Form 为空,但无论如何。就此而言, Request.InputStream 是空的。怎么回事?

【问题讨论】:

    标签: c# angularjs


    【解决方案1】:

    您可能想尝试添加以下标题类型之一:

    应用程序/x-www-form-urlencoded 多部分/表单数据

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 1970-01-01
      • 2021-04-27
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多