C# 我可以手动硬编码ASP.NET web API返回的JSON对象吗?

C# 我可以手动硬编码ASP.NET web API返回的JSON对象吗?,c#,asp.net,json,asp.net-mvc,asp.net-web-api,C#,Asp.net,Json,Asp.net Mvc,Asp.net Web Api,我习惯在Django(类似于Ruby on Rails)中这样做,在某些情况下,我需要硬编码一个JSON响应对象,以便客户端能够进行解释,但我一直在网上到处搜索,想知道如何使用ASP.NET web API实现这一点,但在这方面我找不到任何东西,ASP.NETWebAPI似乎迫使我创建一个类来表示每个URI控制器的JSON响应 例如,我知道手动创建JSON响应的唯一方法是: 1.)我首先需要创建表示响应对象的类 public class XYZ_JSON { public string

我习惯在Django(类似于Ruby on Rails)中这样做,在某些情况下,我需要硬编码一个JSON响应对象,以便客户端能够进行解释,但我一直在网上到处搜索,想知道如何使用ASP.NET web API实现这一点,但在这方面我找不到任何东西,ASP.NETWebAPI似乎迫使我创建一个类来表示每个URI控制器的JSON响应

例如,我知道手动创建JSON响应的唯一方法是:

1.)我首先需要创建表示响应对象的类

public class XYZ_JSON
{
    public string PropertyName { get; set; }
    public string PropertyValue { get; set; }
}
2.)然后我需要正确编写URI控制器,该控制器将返回我刚才定义的“XYZ_JSON”:

// GET: api/ReturnJSON
public XYZ_JSON Get()
{
    XYZ_JSON test = new XYZ_JSON { PropertyName = "Romulus", PropertyValue = "123123" };

    return test;
}
将产生类似以下内容的http响应:

200行
{“PropertyName”:“Romulus”,“PropertyValue”:“123123”}

这个从类到JSON的设计模式非常酷,但是当试图将一个类作为包含许多类的JSON对象返回时,它没有任何帮助,实际上会使情况变得更糟,例如:

public class XYZ_JSON
{
    public string PropertyName { get; set; }
    public string PropertyValue { get; set; }
    public List<ComplexObject> objects { get; set; } // <- do not want
}
上面的设计模式是我试图用ASP.NET web API实现的,这是一种返回半硬编码JSON响应对象的非常简单的方法,它允许我从单个URI返回非常独特和动态的响应。在许多用例中,最多会返回8个完全唯一的类对象列表

而且,如果我想完成的是一种倒退的做事方式,那也没关系。我发布了一个非常成功和稳定的iOS应用程序,它有一个完美的Django后端服务器,以这种方式完美地处理事情,没有任何问题

有人能告诉我如何使用ASP.NET web API返回简单的硬编码JSON响应吗

谢谢

您可以在C#中创建,因此您可以使用其中一个来生成硬编码的结果。例如:

return new JsonResult
{
    Data = new
    {
        result = "success",
        value = "some value"
    }
};

为了澄清,上面的代码是针对ASP.NET MVC的。如果您使用的是Web API,则只需返回数据对象,或使用
IHttpActionResult
。匿名类型部分(新的{})保持不变。

使用匿名对象

public object Get(string id)
{
    // do some SQL querying here to grab the model or what have you.

    if (somethingGoesWrong = true)
    return new {result = "fail"}
    else
    return new {result = "success", value= "some value goes here"}
}

可以使用JsonResult类返回json。Json()方法接受匿名abject,因此不需要创建类

您可以使用泛型JObject返回值,而无需构建完整的类结构,如下所示

public JObject Get(int id)
    {

        return JsonConvert.DeserializeObject<JObject>(@"{""result"":""success"",""value"":""some value goes here""}");


    }
publicjobject-Get(int-id)
{
返回JsonConvert.DeserializeObject(@“{”结果“:”成功“,”值“:”一些值在这里“}”);
}

对于硬编码的响应,为什么不做如下操作呢。返回的JSON内容将不带引号

    public HttpResponseMessage Get()
    {
        string content = "Your JSON content";
        return BuildResponseWithoutQuotationMarks(content);
    }

    private HttpResponseMessage BuildResponseWithoutQuotationMarks(string content)
    {
        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent(content);
        return response;
    }

    private HttpResponseMessage BuildResponseWithQuotationMarks(string content)
    {
        var response = Request.CreateResponse(HttpStatusCode.OK, content);
        return response;
    }

旁白:你真的应该使用状态码。状态码是什么意思?,@DanielA.Whitehttp/rest使用状态码报告不同的案例。比如404表示找到的东西,而200表示结果。@Legends@petelids是的。如果使用Web API,甚至不需要将其包装在
JsonResult
中,只需返回对象(或将其包装在
IHttpActionResult
中)如果Accept标头为“application/xml”,它不会失败,因为匿名对象将无法序列化?是的,如果客户端请求xml,它将失败。这只是为了返回JSON。(但是,如果您确实知道如何使其适用于XML,请将其添加到答案中)。我不会编辑您的答案,因为这是一个很好的答案,而且从声音上看,OP控制着调用者。这是我的用例的最佳答案。它将返回为
text/plain
。如果仍希望以
application/json
的形式返回,则需要添加以下内容:
response.Content.Headers.ContentType=new-MediaTypeHeaderValue(“application/json”)
public JObject Get(int id)
    {

        return JsonConvert.DeserializeObject<JObject>(@"{""result"":""success"",""value"":""some value goes here""}");


    }
    public HttpResponseMessage Get()
    {
        string content = "Your JSON content";
        return BuildResponseWithoutQuotationMarks(content);
    }

    private HttpResponseMessage BuildResponseWithoutQuotationMarks(string content)
    {
        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent(content);
        return response;
    }

    private HttpResponseMessage BuildResponseWithQuotationMarks(string content)
    {
        var response = Request.CreateResponse(HttpStatusCode.OK, content);
        return response;
    }