简体   繁体   English

构建C#Web API - REST

[英]Building a C# Web API - REST

I am about to start a project in C#. 我即将在C#中开始一个项目。 I've never used c# and I was hoping I could get some implementation advice before I make a silly mistake and go down the wrong path. 我从来没有使用过c#,我希望在犯下一个愚蠢的错误并走错路之前我可以得到一些实施建议。

What I am trying to achieve is basically having a C# application on a server that can be accessed via a Web API. 我想要实现的是基本上在服务器上有一个可以通过Web API访问的C#应用​​程序。 This application will take in some string variables and then return a string. 此应用程序将接受一些字符串变量,然后返回一个字符串。 The application will be opening and running some installed programs (not c# programs). 该应用程序将打开并运行一些已安装的程序(而不是c#程序)。

I've read about WCF but I think at first glance this might be overkill as the API I am hoping to create will only have one or two request methods and will return a string. 我已经阅读了有关WCF的内容,但我认为乍一看这可能是过度的,因为我希望创建的API只有一个或两个请求方法,并将返回一个字符串。

What I am really looking for is advice on what I should be using, what to look into and even links to good tutorials on building web services with C# and how I can make the link between a web API to a C# app. 我真正想要的是建议我应该使用什么,要查看什么,甚至链接到使用C#构建Web服务的好教程以及如何在Web API和C#应用之间建立链接。

Thanks all for any advice. 谢谢大家的任何建议。

Skip wcf and asmx. 跳过wcf和asmx。 Instead just implement this stuff through generic handlers (.ashx) files. 相反,只需通过通用处理程序(.ashx)文件实现这些东西。

You have complete control over what comes in and goes out without having to muck around with all the XML garbage. 您可以完全控制进出的内容,而无需使用所有XML垃圾。 We did this a while back and haven't looked back. 我们这样做了一段时间,并没有回头。

In short, I'd use WCF if my endpoints were going to be something other than the web server. 简而言之,如果我的端点不是Web服务器,我会使用WCF。 I'd use asmx if I had to deliver all of the responses back as XML AND I was assured only .net clients would be accessing it. 如果我必须以XML格式提交所有响应,我会使用asmx而且我只保证.net客户端将访问它。

Generic handlers are like .aspx pages but without all of the overhead of the page lifecycle. 通用处理程序类似于.aspx页面,但没有页面生命周期的所有开销。 It gives you get a context object that has access to all of the http post and query string variables and it's up to you to decide what to emit back. 它为您提供了一个上下文对象,可以访问所有的http post和查询字符串变量,由您决定要回放的内容。

They are simple to implement and have none of the "what was that config setting for again?" 它们很容易实现,并且没有“再次配置设置是什么?” issues. 的问题。

Here are a couple pretty good walkthroughs: 以下是一些非常好的演练:
http://swindelles.com/2008/07/11/creating-rest-web-services-in-c-sharp/ http://swindelles.com/2008/07/11/creating-rest-web-services-in-c-sharp/
http://www.codeproject.com/KB/aspnet/RestServicesInASPNET2.aspx http://www.codeproject.com/KB/aspnet/RestServicesInASPNET2.aspx

If you think WCF might be overkill you could implement a simple ASP.NET MVC application that returns data as JSON or XML. 如果您认为WCF可能过度,则可以实现一个简单的ASP.NET MVC应用程序,该应用程序将数据作为JSON或XML返回。

http://omaralzabir.com/create_rest_api_using_asp_net_mvc_that_speaks_both_json_and_plain_xml/ http://omaralzabir.com/create_rest_api_using_asp_net_mvc_that_speaks_both_json_and_plain_xml/

update: Another excellent option is ServiceStack . 更新:另一个很好的选择是ServiceStack I've used it and it's really nice to work with. 我已经习惯了,与你合作真的很棒。

For a simple Web API, WCF is overkill, clunky, operation orientated and designed for SOAP based services (it does Web HTTP, but that was an afterthought). 对于一个简单的Web API,WCF是过度的,笨重的,面向操作的,并且是为基于SOAP的服务而设计的(它实现了Web HTTP,但这是事后的想法)。

The new kid on the block is ASP.NET MVC Web API for lightweight web-orientated architectures. 该块上的新孩子是用于轻量级Web架构的ASP.NET MVC Web API Prepare to see more and more of this young upstart. 准备好看到越来越多的年轻新贵。

You could look into vanilla web services. 您可以查看vanilla Web服务。 I only briefly glanced at it, but this seems like a decent guide . 我只是简单地瞥了一眼,但这似乎是一个不错的指南

You really need to take a look to the WCF Data Services they are easy to implement 您真的需要了解它们易于实现的WCF数据服务

http://msdn.microsoft.com/en-us/data/bb931106 http://msdn.microsoft.com/en-us/data/bb931106

I first met them when I implemented the Dino Esposito examples in March 2010 MSDN Magazine http://msdn.microsoft.com/en-us/magazine/ee336022.aspx 我在2010年3月实施Dino Esposito示例时第一次见到它们MSDN杂志http://msdn.microsoft.com/en-us/magazine/ee336022.aspx

I recommend you first read the Dino Example. 我建议你先阅读Dino示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM