网页API 2 HTTP POST方法 [英] Web API 2 Http Post Method

查看:305
本文介绍了网页API 2 HTTP POST方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很反感不是已经找到了解决这一问题。

I am disgusted not have found a solution to this problem.

我开始使用Web API 2创建一个新的API,只是不能得到这个职位,并投入到工作中。在获取所有和获取单个项目的工作完全正常。

I started creating a new api using Web API 2 and just cannot get the POST and PUT to work. The Get all and Get single item works perfectly fine.

有没有相关的文章的任何地方,以及那些我发现只涉及获取和Web API,但不网页API 2。

There are no related articles anywhere, and those that i've found relates only to Gets and Web API, but not Web API 2.

任何援助将做吧。

    // POST: api/checkOuts
    [HttpPost]
    [ResponseType(typeof(checkOut))]
    [ApiExplorerSettings(IgnoreApi = true)]
    public async Task<IHttpActionResult> PostcheckOut(checkOut co)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.checkOuts.Add(checkOut);

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateException)
        {
            if (checkOutExists(checkOut.id))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return CreatedAtRoute("DefaultApi", new { id = checkOut.id }, checkOut);
    }



所以基本上,我只是试图获得一个调试到方法。

So basically, I'm just attempting to get a debug into the method.

在这个环节特别失望,因为它几乎涵盖了一切,但AI。的 http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing

Was especially disappointed in this link as it covered almost everything, but ai. http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing

问候

推荐答案

这是一个工作代码

        // POST api/values
        [HttpPost]
        [ResponseType(typeof(CheckOut))]
        public async Task<IHttpActionResult> Post([FromBody] CheckOut checkOut)
        {
            if (checkOut == null)
            {
                return BadRequest("Invalid passed data");
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.checkOuts.Add(checkOut);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (checkOutExists(checkOut.id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = checkOut.Id }, checkOut);
        }



我已经声明检验类是这样的:

I've declared CheckOut class to be like this :

public class CheckOut
{
   public int Id { get; set; }
   public string Property2 { get; set; }
}



这里关键的事情是:

The Key things here are :

1,您需要添加[FromBody]你的API方法。
2 - 我已经使用招测试后,通过选择POST操作
I-。
II-内容类型:应用程序/ JSON。
III-传递{ID:1,Property2:任何东西},在邮件正文

1- You need to add [FromBody] to your Api method. 2- I've tested it using Fiddler, i- by choosing POST action. ii- content-type: application/json. iii- passing {"Id":1,"Property2":"Anything"} in the message body.

希望帮助

这篇关于网页API 2 HTTP POST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆