将模型绑定到其他服务器上的JSON API [英] Bind Model to JSON API on other server

查看:58
本文介绍了将模型绑定到其他服务器上的JSON API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET MVC的新手,并且具有返回JSON的现有API.该API存在于另一台服务器上,我需要对API进行服务器到服务器的调用,并将结果数据绑定到模型,以便可以在我正在制作的此Web应用程序的其他部分中使用它.

I'm new to ASP.NET MVC and have an existing API which returns JSON. This API exists on another server and I need to make a server-to-server call to API and bind the resultant data to a Model so that it can be used within other parts of this web app I'm making.

我尝试搜索它,似乎它存在,但是我找不到有关它的基本文档或实现方法.
我可以制作每个组件(发出HTTP请求,解析JSON,设置模型以使用数据),但是如果这已经存在,我不希望重新发明轮子(并且可能做得不好).库.

I tried searching for this and it seems like it exists but I can't find the basic documentation for it or how to implement it.
I could make each component (make HTTP request, parse the JSON, set a model to use the data), but I'd hate to re-invent the wheel (and probably do it poorly) if this is something that is already in the library.

API调用示例:

http://example.info/feeds/feed.aspx?alt=json-in-script

响应:

{
    "version": "1.0",
    "encoding": "UTF-8",
    "feed": {
        "updated": {
            "$t": "2014-07-08T13:58:21-05:00"
        },
        "id": {
            "$t": "http://example.info/feeds/feed.aspx"
        },
        "title": {
            "type": "text",
            "$t": "Example Calendar of Events"
        },
        "link": [
            {
                "rel": "alternate",
                "type": "text/html",
                "href": "http://feed.example.edu/search/"
            },
            {
                "rel": "alternate",
                "type": "application/json",
                "title": "JSON",
                "href": "http://example.info/feeds/feed.aspx?alt=json"
            },
            {
                "rel": "alternate",
                "type": "text/calendar",
                "title": "iCal",
                "href": "http://example.info/feeds/feed.aspx?alt=ical"
            },
            {
                "rel": "self",
                "type": "application/atom+xml",
                "title": "ATOM Feed",
                "href": "http://example.info/feeds/feed.aspx"
            }
        ],
        "author": [
            {
                "name": {
                    "$t": "Example!!"
                },
                "email": {
                    "$t": "web@example.edu"
                }
            }
        ],
        "gd$where": [
            {
                "valueString": "Chicago, IL, US"
            }
        ],
        "gCal$timezone": {
            "value": "America/Chicago"
        },
        "entry": [
            {
                "category": [
                    {
                        "scheme": "http://schemas.google.com/g/2005#kind",
                        "term": "http://schemas.google.com/g/2005#event"
                    },
                    {
                        "term": "Current Students"
                    },
                    {
                        "term": "Faculty"
                    },
                    {
                        "term": "Staff"
                    }
                ],
                "published": {
                    "$t": "2012-03-06T20:57:24+00:00"
                },
                "updated": {
                    "$t": "2012-03-06T20:57:24+00:00"
                },
                "id": {
                    "$t": "http://example.info/feed/?eventid=74289"
                },
                "gCal$uid": {
                    "value": "e72724e9-34eb-41dd-a75a-78d1577cb98a.127924@feed.example.edu"
                },
                "title": {
                    "type": "text",
                    "$t": "Last Day of Sessions 1 & 4 Classes"
                },
                "content": {
                    "type": "html",
                    "$t": "<p>Session 1 &amp; 4 period ends today.</p>"
                },
                "summary": {
                    "type": "text",
                    "$t": "Session 1 & 4 period ends today."
                },
                "author": [
                    {
                        "name": {
                            "$t": "Office"
                        },
                        "email": {
                            "$t": "registrar@example.edu"
                        }
                    }
                ],
                "gd$who": [
                    {
                        "rel": "http://schemas.google.com/g/2005#event.organizer",
                        "valueString": "Registrar, Office of the"
                    },
                    {
                        "rel": "http://schemas.google.com/g/2005#event.attendee",
                        "valueString": "Current Students"
                    },
                    {
                        "rel": "http://schemas.google.com/g/2005#event.attendee",
                        "valueString": "Faculty"
                    },
                    {
                        "rel": "http://schemas.google.com/g/2005#event.attendee",
                        "valueString": "Staff"
                    }
                ],
                "gd$organization": [
                    {
                        "label": "Campus",
                        "primary": "true",
                        "gd$orgName": {
                            "$t": "Chicago"
                        }
                    }
                ],
                "gd": {
                    "value": "http://schemas.google.com/g/2005#event.opaque"
                },
                "link": [
                    {
                        "rel": "alternate",
                        "type": "text/html",
                        "href": "http://feed.example.edu/viewevent.aspx?eventid=74289&occurrenceid=127924"
                    }
                ],
                "gCal$sequence": {
                    "value": "0"
                },
                "gd$when": [
                    {
                        "startTime": "2014-07-30",
                        "endTime": "2014-07-31"
                    }
                ],
                "gd$where": [
                    {
                        "valueString": "Classes administered by the Chicago Campus"
                    }
                ]
            },
            ...
        ]
    }
}

我现在在

I just now found this article on Calling a Web API From a .NET Client, which is in-line with what I'm trying to ask with this question, but I need to know how to do this in an ASP.NET MVC context, not a console application.

推荐答案

要调用外部API,可以使用 HttpClient .就个人而言,我会将对API的调用包装在类似于存储库模式的类中.

To call an external API you can use the HttpClient. Personally, I would wrap the calls to the API in their own class akin to the repository pattern.

public class ApiCaller
{    
    /*
      this is the repository that can wrap calls to the API
      if you have many different types of object returned
      from the API it's worth considering making this generic
    */
    HttpClient client;

    public SomeClass Get()
    {
        SomeClass data;

        string url = "http://example.info/feeds/feed.aspx?alt=json-in-script";

        using (HttpResponseMessage response = client.GetAsync(url).Result)
        {
            if (response.IsSuccessStatusCode)
            {
                data = JsonConvert.DeserializeObject<SomeClass>(response.Content.ReadAsStringAsync().Result);
            }
        }
        return data;
    }
}

然后在控制器中,我将调用ApiCaller以获取所需的对象,在这种情况下,我只是将其传递给视图:

Then in the controller I would call the ApiCaller to get the object required at which point in this instance I'm just passing it to a view:

public class MyController : Controller
{
    ApiCaller caller;

    public MyController()
    {
        //maybe inject this dependency
        ApiCaller = new ApiCaller();
    }

    public ActionResult Index()
    {
        SomeClass model = ApiCaller.Get();

        //do something with the instance if required

        return View(model);
    }
}

然后可以根据需要扩展ApiCaller,以支持帖子,Put等.如果您希望在API上处理许多不同的实体,则可以为每个实体创建一个ApiCaller,或者可以使用泛型.

The ApiCaller can then be extended if required to support posts, puts etc. If you have many different entities on the API that you wish to handle you can make an ApiCaller per entity or you could potentially use generics.

这篇关于将模型绑定到其他服务器上的JSON API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆