C# 使用 ASP.NET MVC 作为 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10778377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:13:23  来源:igfitidea点击:

Using ASP.NET MVC as Web Service

c#asp.net-mvcweb-servicesrest

提问by Henry Aung

Does anyone have experience using ASP.NET MVC project as a Web Service?

有没有人有使用 ASP.NET MVC 项目作为 Web 服务的经验?

i.e. using ASP.NET MVC without Views, so other applications can use the URL to GET or POST to the actions in the Controller.

即使用没有视图的 ASP.NET MVC,因此其他应用程序可以使用 URL 来 GET 或 POST 到控制器中的操作。

Has anyone used it? If so, are there any drawbacks for not using Web Service project instead?

有人用过吗?如果是这样,不使用 Web Service 项目有什么缺点吗?

Thank you all in advance!

谢谢大家!

采纳答案by Brandon Linton

It really depends on the kind of application you're writing. I would actually argue the inverse of LukLed's position - SOAP-based services are better suited for internal clients when you want to support things like Windows Authentication, or different protocols like TCP or MSMQ.

这实际上取决于您正在编写的应用程序类型。我实际上认为与 LukLed 的立场相反——当您想要支持 Windows 身份验证或 TCP 或 MSMQ 等不同协议时,基于 SOAP 的服务更适合内部客户端。

Using a more web-style of GETs and POSTs around specific "resources" starts to get you into the REST architectural style. This technique has a few distinct advantages to me:

围绕特定“资源”使用更多网络风格的 GET 和 POST 开始让您进入REST 架构风格。这种技术对我来说有几个明显的优势:

  • The response is usually smaller, particularly when using lightweight formats like JSON
  • Because of the simplicity of the requests and responses, this makes it much easier to use in mobile / native applications (see Twitter's API, for example)
  • The service you create can be self-describing and discoverable, because you can link to other parts of your API just like normal web pages.
  • 响应通常较小,尤其是在使用 JSON 等轻量级格式时
  • 由于请求和响应的简单性,这使得它更容易在移动/本机应用程序中使用(例如,请参阅Twitter 的 API
  • 您创建的服务可以是自描述和可发现的,因为您可以像普通网页一样链接到 API 的其他部分。

One article that particularly helped me understand the tradeoffs here is Martin Fowler's "Steps Toward the Glory of REST."That being said, it may or may not be the right fit for your application.

一篇特别帮助我理解此处权衡的文章是 Martin Fowler 的“迈向 REST 荣耀的步骤”。话虽如此,它可能适合也可能不适合您的应用。

If you do choose to build a more REST-based service, definitely consider using the ASP.NET Web APIbuilt into MVC4 as others have mentioned. It's currently in beta, but Microsoft felt good enough about it to give it a go-live license.

如果您确实选择构建更基于 REST 的服务,请务必考虑使用其他人提到的内置于 MVC4 中的ASP.NET Web API。它目前处于测试阶段,但微软对它感觉很好,可以给它一个上线许可证。

UPDATE:

更新:

Since ASP.NET core, ASP.NET web API has been integrated into MVC 6 project. https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

从 ASP.NET 核心开始,ASP.NET Web API 已被集成到 MVC 6 项目中。 https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6

回答by LukLed

If you want to use simple GET and POST calls, MVC will be good choice. ASP.NET MVC 4 will have support for creating HTTP based APIs. You can read about it here: http://www.asp.net/web-api

如果您想使用简单的 GET 和 POST 调用,MVC 将是不错的选择。ASP.NET MVC 4 将支持创建基于 HTTP 的 API。你可以在这里阅读它:http: //www.asp.net/web-api

Web Service created in Web Service project can be easier to consume, because it can generate WSDL file, that can be easily read and used in many different languages (by using SOAP protocol). On the other side, WS can create huge XML responses, that could be many times smaller, if you used your own format.

在 Web Service 项目中创建的 Web Service 可以更容易使用,因为它可以生成 WSDL 文件,可以在许多不同的语言(通过使用 SOAP 协议)中轻松阅读和使用。另一方面,WS 可以创建巨大的 XML 响应,如果您使用自己的格式,响应可能会小很多倍。

If you want to spread your web service around the world, allowing SOAP will make life easier for many developers. SOAP can be used by people, who almost have no idea about programming. If you'll use it internally, prefer speed and simple requests and responses, you can use MVC.

如果您想在世界范围内传播您的 Web 服务,那么允许 SOAP 将使许多开发人员的生活更轻松。对编程几乎一无所知的人可以使用 SOAP。如果您将在内部使用它,更喜欢速度和简单的请求和响应,则可以使用 MVC。

回答by fenix2222

New ASP.NET MVC includes Web Api Kit, which can do exactly what you want. With current version you can still use it. There are no real drawbacks to it

新的 ASP.NET MVC 包括 Web Api Kit,它可以完全满足您的需求。使用当前版本,您仍然可以使用它。它没有真正的缺点