简体   繁体   English

如何在MVC 5中包含第三方API-Riot Games

[英]How to include 3rd party API in MVC 5 - Riot Games

I am making a site and I would like to try and pull some user data via a Developer API. 我正在制作一个网站,我想尝试通过开发人员API提取一些用户数据。

I have never done this before, and I am not sure how to really search for a solution. 我以前从未做过此事,也不确定如何真正寻找解决方案。 All I know is that I think I should use AJAX and that I need to make sure it is cross-domain compatible. 我所知道的就是我应该使用AJAX,并且需要确保它与跨域兼容。

The game I am making a stat page for is League of Legends, and they have an API in order to pull their information in JSON format: https://developer.riotgames.com/docs/getting-started 我正在制作统计页面的游戏是《英雄联盟》,并且它们具有API以便以JSON格式提取其信息: https : //developer.riotgames.com/docs/getting-started

Their example uses this, which does: a cURL request loads RiotSchmick's basic summoner object in JSON. 他们的示例使用此方法,该方法可以: cURL请求将JSON中的RiotSchmick的基本召唤器对象加载。

curl --request GET 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/RiotSchmick?api_key=<key>' --include

What I am wondering is if someone could show me a very simple way to include this within my MVC project, like a simple View that contains a single div that will update with RiotSchmick's user data, and then the AJAX/JSON call that does this magic. 我想知道的是,是否有人可以向我展示一种非常简单的方法来将其包含在我的MVC项目中,例如一个简单的View ,其中包含一个div ,它将使用RiotSchmick的用户数据进行更新,然后执行AJAX / JSON调用就可以做到这一点。 You can ignore the <key> since I assume I just plug my key into that location. 您可以忽略<key>因为我假设我只是将密钥插入该位置。

Thank you for all the help! 感谢您的所有帮助!

For beginning asp.net json from url and jquery json parse keywords will help you. 对于asp.net json from urljquery json parse开头的asp.net json from url jquery json parse关键字将为您提供帮助。

I got started before this API but I didn't continue. 我在使用此API之前就已经开始,但没有继续。

Of course you can only download string on your controller via webclient etc and parse it javascript/framework on your view. 当然,您只能通过webclient等在控制器上下载字符串,然后在视图上解析javascript / framework。

Use your own; 用你自己的; Controller name, Model namespace and API key 控制器名称,模型名称空间和API密钥

MODEL 模型

Public Class SummonerModel

 Public Property ID As Integer
 Public Property Name As String
 Public Property ProfileIconId As Integer
 Public Property SummonerLevel As Integer
 Public Property RevisionDate As String
 Public Property ErrorMessage As String

End Class

VIEW 视图

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of MP.Web.Models.Site.SummonerModel)" %>

<div id="SummonerResult">
<%If Model.ErrorMessage = "" Then%>

ID : <%=Model.ID%><br />
Name : <%=Model.Name%><br />
Profile Icon Id : <%=Model.ProfileIconId%><br />
Revision Date : <%=Model.RevisionDate%><br />
SummonerLevel : <%=Model.SummonerLevel%><br />

<%Else%>

Error / <%=Model.ErrorMessage%>

<%End If%>

</div>

**CONTROLLER ** **控制器**

Function GetSummoner(name As String) As PartialViewResult

    Dim model = New SummonerModel

    Dim url = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" & name & "?api_key=<key>"

    Using webClient = New System.Net.WebClient()

        Try

            Dim json = webClient.DownloadString(url)

            Dim dic = (New JavaScriptSerializer()).Deserialize(Of Dictionary(Of String, SummonerModel))(json)

            model = dic.FirstOrDefault.Value

        Catch ex As Exception

            model.ErrorMessage = ex.Message

        End Try

    End Using


    Return PartialView(model)

End Function

SCRIPT 脚本

setInterval(function () { $("#SummonerResult").load("/RiotGamesApi/GetSummoner?name=RiotSchmick") }, 1000);

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

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