使用ASP。NET Web API来自ASP。NET MVC和移动设备

时间:2022-10-11 20:13:28

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

在对这个话题进行了大量徒劳的搜索之后,我需要在这里提出这个问题。

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用户可以访问它,并让它基于web浏览器。

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.

所有这三种类型的客户机(Android、iPhone和web浏览器)都应该能够连接到相同的服务器(在那里实现了游戏引擎),因此使用相同的逻辑和游戏规则——它们的客户端外观是不同的。

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.

我用ASP编程过。这就是我打算在这里使用的服务器端基础设施。具体来说,我看了ASP。NET Web API,它看起来很棒:我的三种类型的客户端只需要从Web API中以json的形式获取游戏数据并呈现出来。

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?

我的问题是:iPhone和Android将如何从Web API获得和使用json格式的游戏数据,这一点很清楚。但是我很不清楚这个网站的版本(我认为是ASP)。NET MVC)会使用Web API吗?仅仅从MVC向Web API发出http请求是明智的吗?

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.

本质上,我想编写一次游戏逻辑,通过Web APi向使用它的许多不同类型的客户端公开它。我想到的这个架构,在图#3中得到了美丽的描述——不能在这里张贴图片。

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

图上的其他人,代表我的iPhone和安卓用户。

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

任何指向其他方向的指针都是受欢迎的。:)谢谢你

1 个解决方案

#1


3  

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

您的API控制器应该非常薄,您可以通过进一步提取业务逻辑来实现这一点,比如:

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

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

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

然后从MVC前端,你基本上可以复制控制器,在GameLogic上执行相同的方法:

public class GameController : Controller
{
    GameLogic _gameLogic;

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

Alternatively, you can let the MVC controllers call the Web API, for example using RestSharp:

或者,您可以让MVC控制器调用Web API,例如使用RestSharp:

public class GameController : Controller
{
    WebAPIClient _webAPIClient;

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

Or you can let MVC be pretty dumb, and let the browser communicate with the WebAPI, for example using jQuery.

或者你可以让MVC变得很笨,让浏览器与WebAPI通信,例如使用jQuery。

#1


3  

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

您的API控制器应该非常薄,您可以通过进一步提取业务逻辑来实现这一点,比如:

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

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

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

然后从MVC前端,你基本上可以复制控制器,在GameLogic上执行相同的方法:

public class GameController : Controller
{
    GameLogic _gameLogic;

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

Alternatively, you can let the MVC controllers call the Web API, for example using RestSharp:

或者,您可以让MVC控制器调用Web API,例如使用RestSharp:

public class GameController : Controller
{
    WebAPIClient _webAPIClient;

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

Or you can let MVC be pretty dumb, and let the browser communicate with the WebAPI, for example using jQuery.

或者你可以让MVC变得很笨,让浏览器与WebAPI通信,例如使用jQuery。