简体   繁体   English

在C#Web API中从Controller清空JSON响应

[英]Empty JSON Response from Controller in C# Web API

I am learning about building web APIs in C#. 我正在学习如何在C#中构建Web API。

I am testing my controller, however I am getting an empty JSON response. 我正在测试我的控制器,但是我得到了一个空的JSON响应。

This is my controller 这是我的控制器

public class ClassInfoController : ApiController
{
    private ClassRepository classRepo = new ClassRepository();

    public IList<string> getClassListByTerm(string termID)
    {
        List<string> classList = classRepo.getClassListByTerm(termID);

        return classList;
    }

This is my repository class which queries my Entity model 这是我的存储库类,它查询我的实体模型

public class ClassRepository : IClass
{

    private cet_databaseEntities db = new cet_databaseEntities();

    public List<string> getClassListByTerm(string termID)
    {
        List<string> result = (from t in db.classCodeLookup
                where t.termID == termID
                orderby t.@class ascending
                select t.@class).ToList<string>();

        return result;
    }
}

I have debugged my controller and when the API call is made, this repository class returns no data to the controller ie an empty list . 我已经调试了我的控制器,并且在进行API调用时,此存储库类不向控制器返回任何数据,即空list However, the reason I am confused, is that when I use the following test code on the repository class 但是,我感到困惑的原因是,当我在存储库类上使用以下测试代码时

public class ClassRepositoryTests
{
    ClassRepository testRepo = new ClassRepository();

    [TestMethod()]
    public void getClassListByTermTest()
    {
        List<string> output = testRepo.getClassListByTerm("316a");
        foreach (string className in output)
        {
            Console.WriteLine(className);
        }
    }
}

I get the result from the repository class I expect. 我从我期望的存储库类中获得结果。

Why am I having this issue? 我为什么遇到这个问题?

Try to add attribute to your API method 尝试向API方法添加属性

[HttpGet]
public List<string> getClassListByTerm([FromUri]string termID)

or you can use another class object for request data. 或者您可以使用另一个类对象来请求数据。 I mean 我的意思是

 public class TermData
 {
     //use if nessassry [JsonProperty("TermId")]
     public string TermId {get;set;}
 }

 [HttpGet]
 public List<string> getClassListByTerm([FromUri]TermData data)

My issue was the automatic API documentation provided by a Web API project in Visual Studio told me that I should call my controller from the browser with the following: http://localhost:51520/api/ClassInfo?termID={316a} . 我的问题是Visual Studio中的Web API项目提供的自动API文档告诉我,我应该使用以下命令从浏览器调用我的控制器: http://localhost:51520/api/ClassInfo?termID={316a} Unbeknownst to me, this meant the {} stayed with the 316a - I thought they were just like the parentheses used in a method signature for receiving a parameter, but I presume they are used for passing list / json to a method (not relevant in this case). 我不知道,这意味着{}保留了316a - 我认为它们就像接收参数的方法签名中使用的括号一样,但我认为它们用于将list / json传递给方法(不相关)这个案例)。 This meant that when my repository query ran, it returned nothing because I was not looking for the string {316a} . 这意味着当我的存储库查询运行时,它没有返回任何内容,因为我没有查找字符串{316a}

If I call the API with the following address http://localhost:51520/api/ClassInfo?termID=316a it works because the string 316a gets passed to the query. 如果我使用以下地址调用API http://localhost:51520/api/ClassInfo?termID=316a它的工作原理是因为字符串316a被传递给查询。

Thank you all. 谢谢你们。

I had a class inheriting from DynamicObject, and that would only return empty JSON (while XML seemed to return properly). 我有一个继承自DynamicObject的类,它只返回空的JSON(而XML似乎正确返回)。

In my case, I was able to remove the DynamicObject inheritance and JSON serialization started working. 在我的情况下,我能够删除DynamicObject继承和JSON序列化开始工作。

//didn't serialize to json, but xml worked
public MyClass : DynamicObject, IMyClass { //... }

//works for json and xml
public MyClass : IMyClass { //... }

Unfortunately, I didn't have a chance to look into why DynamicObject breaks JSON serialization. 不幸的是,我没有机会研究为什么DynamicObject会破坏JSON序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM