简体   繁体   English

ASP.NET C#,将对象返回到Angular

[英]ASP.NET C#, returning objects to Angular

I am writing a C# api backend for an Angular front end. 我正在为Angular前端编写C#API后端。 I have done this before in other projects and previously I have always just returned an object from the c# and in postman or Angular I was able to read the object. 我之前在其他项目中已经做到这一点,以前我总是从c#中返回一个对象,而在邮递员或Angular中,我能够读取该对象。 In postman, it was neatly formatted as json. 在邮递员中,它整齐地格式化为json。

For example, I used to have a class Person and a controller PersonController . 例如,我曾经有一个class Person和一个控制器PersonController In PersonController , I could have a method Get() and the Get would be like this PersonController ,我可以有一个方法Get() ,而Get会像这样

public Person Get() {
    Person p = new Person();
    ...
    return p;
}

and I should call the PersonController.Get from Angular and I would have the person. 我应该从Angular调用PersonController.Get ,然后得到这个人。 However for some reason, now I am doing this in a new project and all I am getting is [Person] -- not the expandable json. 但是由于某种原因,现在我正在一个新项目中执行此操作,我得到的只是[Person]而不是可扩展的json。

Why is this? 为什么是这样?

I know I see that a diff. 我知道我看到了差异。 developer has spent time to return it as a string: 开发人员已花费时间将其作为字符串返回:

JavaScriptSerializer json = new JavaScriptSerializer()
            {
                MaxJsonLength = 3000;
            };
strJson = json.Serialize(model);
return strJson;

But I want to return the actual object. 但是我想返回实际的对象。 I find parsing it made it much much slower. 我发现解析它使它慢得多。

EDIT: 编辑:

This is my actual controller: 这是我的实际控制器:

 public class PersonController : Controller
 {
        private readonly IPersonService personService;

        public PersonController()
        {
            presonService =  new PersonService();
        }

        public async Task<JArray> Get(int? id)
        {
            var model = await personService.GetInfo(id, ConnHelper.ConnectionString());
            JArray a = (JArray)JToken.FromObject(model);
            return a;
            //return model;
        }

        public async Task<List<Person>> GetObject(int? id)
        {
                var model = await personService.GetInfo(id, ConnHelper.ConnectionString());                
                return model;
        }
}

Because your controller is a normal controller you should return Json as below: 因为您的控制器是普通控制器,所以您应该返回Json,如下所示:

public class PersonController : Controller
{
    private readonly IPersonService personService;

    public PersonController()
    {
        presonService =  new PersonService();
    }


    public async Task<JArray> Get(int? id)
    {
        var model = await personService.GetInfo(id, ConnHelper.ConnectionString());
        JArray a = (JArray)JToken.FromObject(model);
        return Json(a);
        //return model;
    }




public async Task<List<Person>> GetObject(int? id)
            {
                var model = await personService.GetInfo(id, ConnHelper.ConnectionString());                
                return Json(model);
            }
        }

When you only need to support JSON in your ASP.NET Web API, in your Global.asax inside Application_Start(), add this 当您只需要在ASP.NET Web API中支持JSON时,请在Application_Start()中的Global.asax中添加此代码

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

I think this will be a good place to dig: https://www.strathweb.com/2013/06/supporting-only-json-in-asp-net-web-api-the-right-way/ 我认为这将是一个挖掘的好地方: https : //www.strathweb.com/2013/06/supporting-only-json-in-asp-net-web-api-the-right-way/

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

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