【问题标题】:Web API server controller doesn't get parametrsWeb API 服务器控制器没有获取参数
【发布时间】:2016-06-12 11:09:22
【问题描述】:

我从 Android 应用发送 JSON 数据。在服务器端我有问题 - Web API 服务器 控制器没有获得参数。从客户端我发送这样的 json 数据:

{   "TypeID ": "1",
    "FullName": "Alex",
    "Title": "AlexTitle",
    "RegionID": "1",
    "CityID ": "1",
    "Phone1": "+7(705)105-78-70"
}

感谢您的帮助,如果您有一些信息,请告诉我想法,谢谢! 这是控制器

 [System.Web.Http.HttpPost]
    public HttpResponseMessage PostRequisition([FromBody]string requisition)
    {
        Requisition postReq = new Requisition();
       if (!String.IsNullOrEmpty(requisition))
        {
            dynamic arr = JValue.Parse(requisition);
            //PostReq model = JsonConvert.DeserializeObject<PostReq>(requisition);
            postReq.FullName = arr.FullName;
            postReq.CityID = Convert.ToInt32(arr.CityID);
            postReq.RegionID = Convert.ToInt32(arr.RegionID);
            postReq.TypeID = Convert.ToInt32(arr.TypeID);
            postReq.UserID = 8;
            postReq.Title = arr.Title;
            postReq.Date = Convert.ToDateTime(arr.Date, CultureInfo.CurrentCulture);
            postReq.Decription = arr.Description;
            postReq.Phone1 = arr.Phone1;
            postReq.Activate = false;
            postReq.ClickCount = 0;
            try
            {
                db.Requisition.Add(postReq);
                db.SaveChanges();
                Message msg = new Message();
                msg.Date = DateTime.Now;
                msg.Type = "POST";
                msg.Text = "OK";
                db.Message.Add(msg);
                db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, postReq);
            }
            catch (Exception ex)
            {
                Message msg = new Message();
                msg.Date = DateTime.Now;
                msg.Type = "POST";
                msg.Text = "ERROR";
                db.Message.Add(msg);
                db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, ex.Message);
            }
        }
        else
        {
            Message msg = new Message();
            msg.Date = DateTime.Now;
            msg.Type = "POST";
            msg.Text = "null";
            db.Message.Add(msg);
            db.SaveChanges();
            return Request.CreateResponse(HttpStatusCode.OK, "null");
        }

    }

【问题讨论】:

  • 这不是asp.net web api,它的C# web api 2.0
  • @Bhargav 抱歉,但在标签中我没有找到任何合适的标签
  • 是的,我刚刚搜索了很奇怪,c# web api 2.0 被标记为 asp.net web api 很奇怪

标签: android json asp.net-web-api


【解决方案1】:

你的问题很简单。您正在发送一个 JSON 对象,但在您的 POST 操作中需要一个字符串。解决此问题的简单方法是创建一个映射到 JSON 对象的类:

public class RequisitionViewModel
{
    public int TypeID {get; set;}
    public string FullName {get; set;}
    public string Title {get; set;}
    public int RegionID {get; set;}
    public int CityID  {get; set;}
    public string Phone1 {get; set;}    
}

然后,将您的操作签名更改为:

[FromBody]RequisitionViewModel requisition)

您也不需要代码中的所有转换:

postReq.FullName = requisition.FullName;
postReq.CityID = requisition.CityID;
//other fields...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多