【问题标题】:how to execute POST and what to return from API controller如何执行 POST 以及从 API 控制器返回什么
【发布时间】:2016-04-22 19:36:31
【问题描述】:

我正在使用 c# .net 4.5 创建我的 Web 应用程序将连接到的 API 控制器。然后,此控制器将数据 POST 到另一个 API。

所以我有我的服务类:

public class PlayerServiceConnector()
{
    private Uri baseUri;
    private String UserName;
    private String Password;

    private string Name;
    private string CharacterRace;
    private string CharacterClass;
    private string AutoSave;
}

public PlayerServiceConnector()
{
    baseUri = new Uri("http://system/dev/API/characterGen");
    UserName = "Joe";
    Password = "12345";

    //just for testing 
    Name = "Mighty Man";
    CharacterRace = "Human";
    CharacterClass = "Warrior";
    AutoSave = true;

    var path = baseUri + "/newCharacter";

    // POST new character data to 3rd party character generating API
    using (var wb = new WebClient())
    {
        wb.Credentials = new NetworkCredential(UserName, Password);
        wb.Headers.Add("Authorization", "Basic " + authInfo);

        var data = new NameValueCollection();
        data["Name"] = Name;
        data["CharacterRace"] = CharacterRace;
        data["CharacterClass"] = CharacterClass;
        data["AutoSave"] = AutoSave.ToString();

        var response = wb.UploadValues(path, "POST", data);

    }
}

我有我的 .NET ApiController (System.Web.Http):

    [Route("api/CharacterGeneration/createNew")]
    public IHttpActionResult PostNewCharacter([FromBody] string value)
    {
        //create new instance of PlayerServiceConnector
        var player = new PlayerServiceConnector();

        //not sure what to do here...
    //how do I get the WebClient part to POST in PlayerServiceConnector?
        //what do I return?

        return NoContent();
    }

我的问题/问题是,当我将浏览器指向“api/CharacterGeneration/createNew”时,如何确保已发布数据以及返回给客户端的内容是什么?这就是我感到困惑和困惑的地方。

谢谢!

【问题讨论】:

  • 我建议对 RestSharp(或类似的)进行一些研究。这对这种事情非常有用。 restsharp.org

标签: c# asp.net-web-api asp.net-apicontroller


【解决方案1】:
    [Route("api/CharacterGeneration")]
    public IHttpActionResult AddNewCharacter([FromBody] Character character)
    {
        if (character == null)
           return BadRequest();

        // put your code for adding the new character here
        // either from your repository or db context

        // return response of 201 if you created the resource successfully
        // typically return this with a uri to the new resource
        return Created("new location", characterYouCreated);
    }

【讨论】:

    【解决方案2】:

    代码示例返回一个字符串列表:

    public IHttpActionResult PostNewCharacter([FromBody] string value)
    {
        var list = new List<string>();
        list.Add("something");
        list.Add("something else");
        return Json(list);
    }
    

    在浏览器上运行,将以 Json 结构打印字符串

    从您的连接器运行将返回您可以解析到代理类的 json 结构。

    如果您想要一个框架来解析 Json,请使用 http://www.newtonsoft.com/json 或在 Visual Studio 内的 NuGet 上找到 newtonsoft。我认识的每个人都在使用 newtonsoft 作为 Json

    【讨论】:

    • 我对“返回”部分感到困惑,因为我正在将数据发布到 API 以供系统处理...如何处理返回的 Json 列表?谢谢!
    • @999cm999 您在哪里将数据发布到 API?你在哪里调用createNew 方法?
    • @999cm999 您将使用我发送给您的框架中的 JsonConvert.DeserializeObject 进行反序列化
    • @ThePoet 但这如何使用我在 PlayerServiceConnector() 方法中的代码?这就是我所有功能的核心所在,即与 3rd 方 API 交互。
    【解决方案3】:

    关于您问题的返回部分,您可以指向您发布的数据。

    return Redirect(new Uri(url), HttpStatusCode.Created);
    

    其中 url 将是 Get 路由的 url 以获取您刚刚发布的数据。

    var url = Url.Link("Name of your Get route", new object());
    

    你的对象会根据你的get route的参数要求而有所不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-10
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      相关资源
      最近更新 更多