我需要(对我自己)提供一种从数据库中获取一些数据的方法,以便我可以轻松构建静态 html 页面,这样我就可以 减轻服务器处理它们的负担 (因为我每天浪费 80Gb 的带宽)。
我的问题很简单
我应该创建 WCF 服务 (或 ASMX Web 服务)来提取这些数据(WCF 将在同一台服务器中,所以我仍然使用它,尽管更少,因为我只是得到我真正需要的东西并且不会浪费处理页面)
或者我应该创建一个简单的 Controller 像:
public class ServiceApiController : Controller
{
public ActionResult GetPrizes(string calendarGuid)
{
return Json("...");
}
public ActionResult GetWinners(string calendarGuid)
{
return Json("...");
}
public ActionResult AddSubscriber(string calendarGuid, string[] args)
{
return Json("...");
}
public ActionResult ReclaimSubscriberEmail(string calendarGuid, string email)
{
return Json("...");
}
public ActionResult RequestContact(string calendarGuid, string[] args)
{
return Json("...");
}
}
并直接从我将使用的任何 Javascript 框架中调用它(错误,当然是我可爱的 jQuery)。
最佳答案
依靠。 WCF 服务更加灵活,因为您不必依赖 HTTP,而是可以使用直接 TCP 连接,而且我相信让它公开 SOAP 真的很容易。只是行为、端点等的 web.config 设置比它应该的要复杂一些。
另一方面,像这样设置 MVC 可能更快,因为您不必处理有时很难更改的 DataContracts(因为它们需要重新部署共享程序集)。
WCF = 更“稳固”,带有更多协议(protocol)以使其“稳定”,即:您可以在 Provider 和 Consumer 之间建立硬契约。
MVC = 更灵活,可能更容易设置,但没有硬性契约(Contract) - 如果您更改 Json 的格式,您的客户可能会在您最初注意到它的情况下中断,因此它更能跟上内容。
总的来说,我使用 MVC,除非我有如上所述使用 WCF 的理由(客户端和服务器之间需要稳定/有保证/可发现的契约(Contract),需要有 SOAP,或者设置 IIS 网站不方便/不必要时,例如因为我的服务器作为 Windows 服务运行)
关于asp.net-mvc - WebService 还是简单的 MVC Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4385854/