从db调用返回.net Web Api的结果

时间:2021-11-17 10:49:55

I'm new to the .net Web API and am trying to figure out how I return a Get result from a call to my database. I know everything works in my regular MVC page. But Not sure how to return the result from the Web API controller. I thought it was as simple as returning Json with the result. Here is my code:

我是.net Web API的新手,我试图找出如何通过调用我的数据库返回Get结果。我知道一切都在我的常规MVC页面中有效。但不确定如何从Web API控制器返回结果。我认为这就像返回Json一样简单。这是我的代码:

    // GET api/<controller>
    public IEnumerable<string> Get()

    {

        using (var _db = new JobsDatabaseEntities())
        {
            var user = Env.CurrentUser;
            var posts =
                _db.JobPostings.Where(
                    j =>
                        j.City.Equals(user.City, StringComparison.OrdinalIgnoreCase) &&
                        j.Industry.ID == user.Industry.ID);
            var result = new List<BusJobPost>();

            foreach (var post in posts)
            {
                var p = new BusJobPost(post);
                result.Add(p);
            }

            return Json(result);
        }

    }

1 个解决方案

#1


0  

Please visit this resource: Action Results in Web API 2.
Your case is described in fourth section Some other type (which is applicable to first version of Web API as well).

请访问此资源:Web API中的操作结果2.您的案例在第四部分中描述了其他一些类型(也适用于Web API的第一版)。

In Web API you don't return JSON result explicitly. It is actually done by process called Content Negotiation. You can read about it here [Content Negotiation in ASP.NET Web API] in detail.

在Web API中,您不会显式返回JSON结果。它实际上是由称为内容协商的过程完成的。您可以在此处详细了解[ASP.NET Web API中的内容协商]。

Highlighting briefly just some of this:

简要介绍一下这个:

  • You can set Accept header for you request as: Accept: application/json (for example, if you use jquery ajax function: dataType: 'json')
  • 您可以为您的请求设置Accept标头:Accept:application / json(例如,如果使用jquery ajax函数:dataType:'json')

  • Or even if you don't use Accept header at all, if you send request with JSON data, you should also get response back in JSON format
  • 或者即使您根本不使用Accept标头,如果您使用JSON数据发送请求,您也应该以JSON格式获得响应

So you should just return result variable from your controller action and satisfy some of conditions to get response serialized into JSON.

因此,您应该从控制器操作返回结果变量,并满足一些条件以将响应序列化为JSON。

#1


0  

Please visit this resource: Action Results in Web API 2.
Your case is described in fourth section Some other type (which is applicable to first version of Web API as well).

请访问此资源:Web API中的操作结果2.您的案例在第四部分中描述了其他一些类型(也适用于Web API的第一版)。

In Web API you don't return JSON result explicitly. It is actually done by process called Content Negotiation. You can read about it here [Content Negotiation in ASP.NET Web API] in detail.

在Web API中,您不会显式返回JSON结果。它实际上是由称为内容协商的过程完成的。您可以在此处详细了解[ASP.NET Web API中的内容协商]。

Highlighting briefly just some of this:

简要介绍一下这个:

  • You can set Accept header for you request as: Accept: application/json (for example, if you use jquery ajax function: dataType: 'json')
  • 您可以为您的请求设置Accept标头:Accept:application / json(例如,如果使用jquery ajax函数:dataType:'json')

  • Or even if you don't use Accept header at all, if you send request with JSON data, you should also get response back in JSON format
  • 或者即使您根本不使用Accept标头,如果您使用JSON数据发送请求,您也应该以JSON格式获得响应

So you should just return result variable from your controller action and satisfy some of conditions to get response serialized into JSON.

因此,您应该从控制器操作返回结果变量,并满足一些条件以将响应序列化为JSON。