c# - 如何将对象从 C# webapp 发送到 asp.net webapi

标签 c# asp.net post windows-runtime asp.net-web-api

连续 3 天我试图将我的对象发送到我的 webapi。我只是无法从 C# 做到这一点:/

我正在尝试 PostAsync 方法,我正在尝试发送 json (但后来我无法反序列化),我正在尝试使用 JObject 但无法对其执行任何操作。我只是不知道该怎么做。

我有一个简单的方法

    public string PostMethod(Location param)
    {
        var test = param;

        return test.name;
    }

如何从 C# 发送 Location 对象,这样我就不会在 webapi 中得到 null ?我唯一实现的就是发送一个简单的类型,如“字符串”。但我的方法都不适用于更复杂的对象。

位于位置对象下方

public partial class Location
{
    public Location()
    {
        this.Category = new HashSet<Category>();
    }

    public int id { get; set; }
    public Nullable<System.DateTime> create_date { get; set; }
    public Nullable<System.DateTime> modify_date { get; set; }
    public Nullable<int> create_user_id { get; set; }
    public Nullable<int> modify_user_id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public Nullable<int> photo_id { get; set; }
    public string www { get; set; }
    public string count_data { get; set; }
    public string status { get; set; }
    public Nullable<double> latitude { get; set; }
    public Nullable<double> longitude { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string zip { get; set; }
    public string telephone { get; set; }
    public string street { get; set; }

    public virtual AzureMedia AzureMedia { get; set; }
    public virtual Users Users { get; set; }
    public virtual Users Users1 { get; set; }
    public virtual ICollection<Category> Category { get; set; }
}

最佳答案

您是否安装了 NuGet 的最新 Web API?为此,您还需要 Json.NET。

这应该可以解决问题:

    Location loc = new Location( ..... );

    var jsonFormatter = new JsonMediaTypeFormatter();

    HttpContent content = new ObjectContent<Location>(loc , jsonFormatter);

    HttpResponseMessage responseMessage = client.PostAsync(uri, content).Result;

关于c# - 如何将对象从 C# webapp 发送到 asp.net webapi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12704571/

相关文章:

javascript - 从 $.post() 函数中获取 2 个变量

java - C# 中相当于 Java 整数文字前缀 0b 的是什么?

c# - Ninject 属性注入(inject)设置问题

c# 使用 Linq2Sql 在表列中查找匹配词

javascript - 防止从 ASP NET 按钮关闭模式

asp.net - 如何导出 "IIS Express Development Certificate"到另一台电脑通过https连接本地IIS?

google-app-engine - 部署在 Google App Engine 上的测试服务器

javascript - 如何获得一个消息框?

c# - 在 MahApps.Metro 中使用数据绑定(bind) ItemsControl 时显示 WindowCommands 的分隔符

c# - 在 ASP.NET Web 应用程序中创建线程的正确方法