从ASP.NET MVC和移动设备消费的ASP.NET Web API [英] Consuming ASP.NET Web API from ASP.NET MVC and mobile devices

查看:165
本文介绍了从ASP.NET MVC和移动设备消费的ASP.NET Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在关于这一主题白白搜索了很多,我需要在这里问的问题。

After alot of in vain search on this topic, I need to ask the question here.

我打算创建一个在线多人游戏卡。最初,游戏只是要通过Android手机打(客户端)。但后来,我也想使其iPhone用户访问以及使基于网络浏览器。

I am planning on creating an online multiplayer cards game. Initially, the game is only going to played via Android phones (as clients). But later, I would also like to make it accessible to iPhone users as well as make it web-browser based.

所有这三种类型的客户端(Android,iPhone和网络浏览器)的应能连接到相同的服务器(游戏引擎中实现),因此使用相同的逻辑和游戏的规则 - 他们的客户端的外观是什么将是不同的。

All these three types of clients (Android, iPhone and web browser) should be able to connect to the same server (where game engine is implemented) and therefore use the same logic and game-rules - their client-side appearance is whats going to be different.

我在编程ASP.NET,这就是我计划在这里使用的服务器端架构。具体来说,我已经看过了的ASP.NET Web API,它看起来很棒:我的三个类型的客户会的只是的需要从Web API作为JSON获取游戏数据和渲染。

I have programmed in ASP.NET and that is what I am planning to use here as the server-side infrastructure. Specifically, I have looked the ASP.NET Web API and it looks great: my three types of clients would simply need to get game data from the Web API as json and render it.

下面是我的问题:
它清楚地知道如何iPhone和Android将获得并使用来自Web的API,JSON格式的游戏数据。但我在网站上的版本如何(我认为这将是ASP.NET MVC)将使用Web API非常不清楚?它是明智的,简单地使HTTP请求的Web API从MVC - ?如果是的话,岂不使慢

Here is my question: Its clear to me how iPhone and Android will get and use that json-formatted game data from the Web API. But I am very unclear on how the website version (which I think is going to be ASP.NET MVC) would consume the Web API? Is it wise to simply make http requests to Web API from MVC - if yes, would it not make it slow?

从本质上讲,我想一次编写游戏逻辑,通过网页API将其暴露在许多不同种类的消耗它的客户。这种架构,我在心中,在图#精美,描绘3 <一个href=\"http://odeto$c$c.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx\"相对=nofollow>此处 - 斜面后图像这里

Essentially, I want to write the game logic once, expose it through Web APi to many different kinds of clients that consume it. This architecture, that I have in mind, is beautifully depicted in diagram #3 here - cant post images here.

所以的其他的图表那里,再present我的iPhone和Android用户上。

So Others on the diagram there, represent my iPhone and Android users.

在其他方向上的任何指针也欢迎。 :)
谢谢

Any pointers in other directions are also welcome. :) Thank you

推荐答案

您的API控制器应该非常薄,你可以通过提取业务逻辑,甚至更远实现的,是这样的:

Your API controllers should be very thin, which you can achieve by extracting your business logic even further away, something like:

public class GameController : ApiController
{
    GameLogic _gameLogic; // inject through constructor

    public PlayCardResult PlayCard(Card card)
    {
        return _gameLogic.PlayCard(card);       
    }   
}

然后从你的MVC前端,可以基本照搬控制器,执行对GameLogic同样的方法:

Then from your MVC front end, you can basically copy the controllers, performing the same methods on the GameLogic:

public class GameController : Controller
{
    GameLogic _gameLogic;

    [HttpPost]
    public ActionResult PlayCard(Card card)
    {
        var model = _gameLogic.PlayCard(card);
        return View(model);
    }   
}

另外,你可以让MVC控制器使用调用Web API,例如 RestSharp

public class GameController : Controller
{
    WebAPIClient _webAPIClient;

    [HttpPost]
    public void PlayCard(Card card)
    {
        var model = _webAPIClient.PlayCard(card);
        return View(model);         
    }   
}

或者你可以让MVC是pretty哑,并让与的WebAPI浏览器的通信,例如的使用jQuery

这篇关于从ASP.NET MVC和移动设备消费的ASP.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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