它应该是WebAPI还是asmx

时间:2022-12-03 22:51:38

Should I leverage an ASMX service or the ASP.NET Web API for my two simple API's?

我应该为我的两个简单的API利用ASMX服务或ASP.NET Web API吗?

I want to create two simple APIs in my ASP.NET MVC project. One takes in 3 parameters (currentUserID, DataType, ActionName). It returns them and an XML string of the data they have requested. The API is consumed by client-side JavaScript code. The other API receives an XML string and uses that on the server side to perform actions on the database.

我想在我的ASP.NET MVC项目中创建两个简单的API。一个接受3个参数(currentUserID,DataType,ActionName)。它返回它们以及它们请求的数据的XML字符串。 API由客户端JavaScript代码使用。另一个API接收XML字符串,并在服务器端使用该字符串对数据库执行操作。

3 个解决方案

#1


20  

I just answered a related question:

我刚刚回答了一个相关的问题:

What is the future of ASP.NET MVC framework after releasing the asp.net Web API

在发布asp.net Web API之后,ASP.NET MVC框架的未来是什么?

Basically, the frameworks provided by Microsoft to develop Web Services are:

基本上,Microsoft提供的用于开发Web服务的框架是:

  • ASMX. XML Services based on SOAP.

    ASMX。基于SOAP的XML服务。

  • WCF. Web services based on SOAP. These services were the evolution of the traditional ASMX services and basically they focused to separate the service itself from the transport protocol. That's why you can expose the same service using several endpoints and therefore several protocols (TCP, HTTP, Named Pipes, MSMQ, HTTPS). This flexibility came with the configuration problem. One of the main complaints in the community about the WCF is the tedious and extensive configuration

    WCF。基于SOAP的Web服务。这些服务是传统ASMX服务的发展,基本上它们专注于将服务本身与传输协议分开。这就是为什么您可以使用多个端点以及多个协议(TCP,HTTP,命名管道,MSMQ,HTTPS)公开相同的服务。这种灵活性伴随着配置问题。社区关于WCF的主要抱怨之一是繁琐而广泛的配置

  • WEB API. Based on HTTP not in SOAP. This new API is a new framework to create services. The main difference with the other two predecesors, is the fact that it's based on HTTP and not on SOAP, therefore you can use several HTTP features like:

    WEB API。基于HTTP而不是SOAP。这个新API是一个用于创建服务的新框架。与其他两个前置者的主要区别在于它基于HTTP而不是SOAP,因此您可以使用以下几种HTTP功能:

    • It contains message headers that are very meaningful and descriptive - headers that suggest the content type of the message’s body, headers that explain how to cache information, how to secure it etc.
    • 它包含非常有意义和描述性的消息头 - 表示消息正文的内容类型的标题,解释如何缓存信息的标题,如何保护信息等。
    • use of verbs to define the actions (POST, PUT, DELETE..)
    • 使用动词来定义动作(POST,PUT,DELETE ..)
    • it contains a body that can be used to send any kind of content
    • 它包含一个可用于发送任何类型内容的正文
    • It uses URIs for identifying both information paths (resources) and actions
    • 它使用URI来识别信息路径(资源)和操作

    WEB API is focused on writing services to expose them via HTTP (only on HTTP at the moment). If you want to expose your service using another protocol, then you should consider using WCF.

    WEB API专注于编写服务以通过HTTP公开它们(目前仅在HTTP上)。如果要使用其他协议公开服务,则应考虑使用WCF。

    WEB API is based on MVC (if you want to know the reasons why it's based on MVC, they are simple)

    WEB API基于MVC(如果你想知道它基于MVC的原因,它们很简单)

    Another goal of the WCF Web APIs was to incorporate known concepts that would help developers to overcome some of the drawbacks they faced with WCF, such as huge configurations, overuse of attributes, and the WCF infrastructure that did not support testing well. Thus the Web APIs used IoC, enabled convention-over-configuration, and tried to offer simpler configuration environment.

    WCF Web API的另一个目标是整合已知的概念,这些概念可以帮助开发人员克服他们在WCF中遇到的一些缺点,例如巨大的配置,过度使用属性以及不支持测试的WCF基础架构。因此,Web API使用IoC,启用了配置约定,并尝试提供更简单的配置环境。

    ASP.NET MVC infrastructure with its elegant handling of HTTP requests and responses, and its support of easy-to-create controllers seemed like the proper way to go for creating this new type of services.

    ASP.NET MVC基础架构优雅地处理HTTP请求和响应,并且支持易于创建的控制器,这似乎是创建这种新型服务的正确方法。

Take the following points into consideration to choose between WCF or WEB API

请考虑以下几点,以便在WCF或WEB API之间进行选择

  • If your intention is to create services that support special scenarios – one way messaging, message queues, duplex communication etc, then you’re better of picking WCF
  • 如果您打算创建支持特殊方案的服务 - 单向消息传递,消息队列,双工通信等,那么您最好选择WCF
  • If you want to create services that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transports are unavailable, then you’re better off with WCF and using both SOAP-based bindings and the WebHttp binding.
  • 如果您想创建可用的快速传输通道的服务,例如TCP,命名管道,甚至UDP(在WCF 4.5中),并且您还希望在所有其他传输不可用时支持HTTP,那么您就是最好使用WCF并使用基于SOAP的绑定和WebHttp绑定。
  • If you want to create resource-oriented services over HTTP that can use the full features of HTTP – define cache control for browsers, versioning and concurrency using ETags, pass various content types such as images, documents, HTML pages etc., use URI templates to include Task URIs in your responses, then the new Web APIs are the best choice for you.
  • 如果您想通过HTTP创建面向资源的服务,可以使用HTTP的全部功能 - 使用ETag定义浏览器的缓存控制,版本控制和并发,传递各种内容类型,如图像,文档,HTML页面等,使用URI模板要在您的回复中包含任务URI,那么新的Web API是您的最佳选择。
  • If you want to create a multi-target service that can be used as both resource-oriented service over HTTP and as RPC-style SOAP service over TCP – talk to me first, so I’ll give you some pointers.
  • 如果你想创建一个多目标服务,既可以用作HTTP上的面向资源的服务,也可以用作TCP上的RPC式SOAP服务 - 请先与我联系,所以我会给你一些指示。

For a more detailed comparison:

有关更详细的比较:

http://www.codeproject.com/Articles/341414/WCF-or-ASP-NET-Web-APIs-My-two-cents-on-the-subjec

http://www.codeproject.com/Articles/341414/WCF-or-ASP-NET-Web-APIs-My-two-cents-on-the-subjec

#2


0  

If possible, I would use an Web Api Controller in mvc4. You can return an generic ienumerable list or model and it will automatically output the data to whatever format is requested such as xml or json. Its pretty amazing.

如果可能的话,我会在mvc4中使用Web Api控制器。您可以返回通用的可枚举列表或模型,它会自动将数据输出到请求的任何格式,如xml或json。它非常棒。

#3


0  

It seems you are really doing much with Views so I think Web API would be more concise solution at this point.

看起来你真的在使用Views做了很多事情,所以我认为Web API在这一点上会更加简洁。

#1


20  

I just answered a related question:

我刚刚回答了一个相关的问题:

What is the future of ASP.NET MVC framework after releasing the asp.net Web API

在发布asp.net Web API之后,ASP.NET MVC框架的未来是什么?

Basically, the frameworks provided by Microsoft to develop Web Services are:

基本上,Microsoft提供的用于开发Web服务的框架是:

  • ASMX. XML Services based on SOAP.

    ASMX。基于SOAP的XML服务。

  • WCF. Web services based on SOAP. These services were the evolution of the traditional ASMX services and basically they focused to separate the service itself from the transport protocol. That's why you can expose the same service using several endpoints and therefore several protocols (TCP, HTTP, Named Pipes, MSMQ, HTTPS). This flexibility came with the configuration problem. One of the main complaints in the community about the WCF is the tedious and extensive configuration

    WCF。基于SOAP的Web服务。这些服务是传统ASMX服务的发展,基本上它们专注于将服务本身与传输协议分开。这就是为什么您可以使用多个端点以及多个协议(TCP,HTTP,命名管道,MSMQ,HTTPS)公开相同的服务。这种灵活性伴随着配置问题。社区关于WCF的主要抱怨之一是繁琐而广泛的配置

  • WEB API. Based on HTTP not in SOAP. This new API is a new framework to create services. The main difference with the other two predecesors, is the fact that it's based on HTTP and not on SOAP, therefore you can use several HTTP features like:

    WEB API。基于HTTP而不是SOAP。这个新API是一个用于创建服务的新框架。与其他两个前置者的主要区别在于它基于HTTP而不是SOAP,因此您可以使用以下几种HTTP功能:

    • It contains message headers that are very meaningful and descriptive - headers that suggest the content type of the message’s body, headers that explain how to cache information, how to secure it etc.
    • 它包含非常有意义和描述性的消息头 - 表示消息正文的内容类型的标题,解释如何缓存信息的标题,如何保护信息等。
    • use of verbs to define the actions (POST, PUT, DELETE..)
    • 使用动词来定义动作(POST,PUT,DELETE ..)
    • it contains a body that can be used to send any kind of content
    • 它包含一个可用于发送任何类型内容的正文
    • It uses URIs for identifying both information paths (resources) and actions
    • 它使用URI来识别信息路径(资源)和操作

    WEB API is focused on writing services to expose them via HTTP (only on HTTP at the moment). If you want to expose your service using another protocol, then you should consider using WCF.

    WEB API专注于编写服务以通过HTTP公开它们(目前仅在HTTP上)。如果要使用其他协议公开服务,则应考虑使用WCF。

    WEB API is based on MVC (if you want to know the reasons why it's based on MVC, they are simple)

    WEB API基于MVC(如果你想知道它基于MVC的原因,它们很简单)

    Another goal of the WCF Web APIs was to incorporate known concepts that would help developers to overcome some of the drawbacks they faced with WCF, such as huge configurations, overuse of attributes, and the WCF infrastructure that did not support testing well. Thus the Web APIs used IoC, enabled convention-over-configuration, and tried to offer simpler configuration environment.

    WCF Web API的另一个目标是整合已知的概念,这些概念可以帮助开发人员克服他们在WCF中遇到的一些缺点,例如巨大的配置,过度使用属性以及不支持测试的WCF基础架构。因此,Web API使用IoC,启用了配置约定,并尝试提供更简单的配置环境。

    ASP.NET MVC infrastructure with its elegant handling of HTTP requests and responses, and its support of easy-to-create controllers seemed like the proper way to go for creating this new type of services.

    ASP.NET MVC基础架构优雅地处理HTTP请求和响应,并且支持易于创建的控制器,这似乎是创建这种新型服务的正确方法。

Take the following points into consideration to choose between WCF or WEB API

请考虑以下几点,以便在WCF或WEB API之间进行选择

  • If your intention is to create services that support special scenarios – one way messaging, message queues, duplex communication etc, then you’re better of picking WCF
  • 如果您打算创建支持特殊方案的服务 - 单向消息传递,消息队列,双工通信等,那么您最好选择WCF
  • If you want to create services that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transports are unavailable, then you’re better off with WCF and using both SOAP-based bindings and the WebHttp binding.
  • 如果您想创建可用的快速传输通道的服务,例如TCP,命名管道,甚至UDP(在WCF 4.5中),并且您还希望在所有其他传输不可用时支持HTTP,那么您就是最好使用WCF并使用基于SOAP的绑定和WebHttp绑定。
  • If you want to create resource-oriented services over HTTP that can use the full features of HTTP – define cache control for browsers, versioning and concurrency using ETags, pass various content types such as images, documents, HTML pages etc., use URI templates to include Task URIs in your responses, then the new Web APIs are the best choice for you.
  • 如果您想通过HTTP创建面向资源的服务,可以使用HTTP的全部功能 - 使用ETag定义浏览器的缓存控制,版本控制和并发,传递各种内容类型,如图像,文档,HTML页面等,使用URI模板要在您的回复中包含任务URI,那么新的Web API是您的最佳选择。
  • If you want to create a multi-target service that can be used as both resource-oriented service over HTTP and as RPC-style SOAP service over TCP – talk to me first, so I’ll give you some pointers.
  • 如果你想创建一个多目标服务,既可以用作HTTP上的面向资源的服务,也可以用作TCP上的RPC式SOAP服务 - 请先与我联系,所以我会给你一些指示。

For a more detailed comparison:

有关更详细的比较:

http://www.codeproject.com/Articles/341414/WCF-or-ASP-NET-Web-APIs-My-two-cents-on-the-subjec

http://www.codeproject.com/Articles/341414/WCF-or-ASP-NET-Web-APIs-My-two-cents-on-the-subjec

#2


0  

If possible, I would use an Web Api Controller in mvc4. You can return an generic ienumerable list or model and it will automatically output the data to whatever format is requested such as xml or json. Its pretty amazing.

如果可能的话,我会在mvc4中使用Web Api控制器。您可以返回通用的可枚举列表或模型,它会自动将数据输出到请求的任何格式,如xml或json。它非常棒。

#3


0  

It seems you are really doing much with Views so I think Web API would be more concise solution at this point.

看起来你真的在使用Views做了很多事情,所以我认为Web API在这一点上会更加简洁。