在 C# 或 ASP.Net 中启动 REST Web 服务的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1157141/
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-06 09:35:08  来源:igfitidea点击:

Where to start REST web service in C# or ASP.Net

c#rest

提问by David.Chu.ca

I am thinking to start writing some REST web services as a way to provide data. I guess that when my REST web services are available, then some of my web applications and console applications will be able to use REST web service as data service to get, add, update and delete data to databases. In addition to that, I would like to add authentication feature to identify any request.

我正在考虑开始编写一些 REST Web 服务作为提供数据的一种方式。我猜想当我的 REST web 服务可用时,我的一些 web 应用程序和控制台应用程序将能够使用 REST web 服务作为数据服务来获取、添加、更新和删除数据库中的数据。除此之外,我想添加身份验证功能来识别任何请求。

My question is that where should I start? I saw Microsoft ADO.Net Data Services. Not sure if this is a good start place? Are there any examples available?

我的问题是我应该从哪里开始?我看到了Microsoft ADO.Net 数据服务。不确定这是否是一个好的开始?有没有可用的例子?

采纳答案by JP Alioto

Check out the REST in WCF MSDN siteand the starter kit. Good article heretoo.

查看WCF MSDN 站点中REST入门工具包这里的文章也不错。

回答by Thomas Beck

  • If you're going WCF, the WCF REST Starter Kit that JP referred to is a great place to start.
  • Omar Al Zabir provides a pretty good exampleof using ASP.NET MVC to provide RESTful services that are fluent in both XML and JSON
  • You can also go the ADO.NET Data Services route you suggested. These services are built on top of the WCF stack.
  • 如果您要使用 WCF,那么 JP 提到的 WCF REST Starter Kit 是一个很好的起点。
  • Omar Al Zabir提供了一个很好的示例,说明使用 ASP.NET MVC 提供流利的 XML 和 JSON 的 RESTful 服务
  • 您也可以使用您建议的 ADO.NET 数据服务路线。这些服务构建在 WCF 堆栈之上。

I've never stumbled across any really good guidance on how to select between these options. In ASP.NET MVC you take on the majority of the plumbing burden but also have maximal control. Straight RESTful WCF is the happy middle ground although WCF tends to want to have things done its way. ADO.NET Data Services are pretty magical with the downside of buying fully into a given approach to generating these services and losing more flexibility.

我从来没有偶然发现任何关于如何在这些选项之间进行选择的真正好的指导。在 ASP.NET MVC 中,您承担了大部分管道负担,但也有最大的控制权。直接的 RESTful WCF 是快乐的中间地带,尽管 WCF 倾向于按照自己的方式完成任务。ADO.NET 数据服务非常神奇,它的缺点是完全采用给定的方法来生成这些服务并失去更多的灵活性。

There are a couple of good books you can read on the topic of RESTful services with .NET. Both O'Reilly and Microsoft Press have recently released books on this topic. Perhaps the most important advice I can provide you is to consume and understand several open RESTful services (e.g. Twitter, Amazon, Flickr) to understand the design decisions that went into creating the services. User provisioning, authentication mechanism, and supported content types (e.g. JSON, XML, RSS/ATOM) are some of the decisions that you can observe in action to aid you in your path to creating your service API.

您可以阅读几本关于 .NET RESTful 服务主题的好书。O'Reilly 和 Microsoft Press 最近都出版了有关此主题的书籍。也许我可以为您提供的最重要的建议是使用和理解几个开放的 RESTful 服务(例如 Twitter、Amazon、Flickr)来理解创建服务的设计决策。用户配置、身份验证机制和支持的内容类型(例如 JSON、XML、RSS/ATOM)是您可以在行动中观察到的一些决策,以帮助您创建服务 API。

回答by Darrel Miller

If you are new to REST in the .net world then start with OpenRasta. The other Microsoft solutions can do REST if you work hard at it but they will guide you down a route where you will most likely end up with POD(Plain old data) over HTTP. That is not what REST is all about. If that's all you want then that's cool too, but it is not REST.

如果您是 .net 世界中的 REST 新手,请从OpenRasta开始。如果您努力工作,其他 Microsoft 解决方案可以执行 REST,但它们会引导您沿着一条路线走下去,最终您很可能会通过 HTTP 获得 POD(纯旧数据)。这不是 REST 的全部内容。如果这就是你想要的,那也很酷,但它不是 REST。

回答by mythz

You may also want to check out servicestack.netAn Open Source, cross-platform, high-performance web service framework that lets you develop web services using code-first, strongly-typed DTO's which will automatically (without any configuration) be immediately available on a variety of different endpoints out-of-the-box (i.e. XML, JSON, JSV, SOAP 1.1/1.2).

您可能还想查看servicestack.net一个开源、跨平台、高性能的 Web 服务框架,它允许您使用代码优先的强类型 DTO 开发 Web 服务,该 DTO 将自动(无需任何配置)立即可用在各种开箱即用的不同端点上(即 XML、JSON、JSV、SOAP 1.1/1.2)。

REST, RPC and SOAP out of the box

REST、RPC 和 SOAP 开箱即用

In addition, your same web services can also be made available via any ReST-ful url of your choice where the preferred serialization format can be specified by your REST client i.e.

此外,您还可以通过您选择的任何 ReST-ful url 提供相同的 Web 服务,您的 REST 客户端可以在其中指定首选的序列化格式,即

  • Using the HTTP Accept:header
  • Appending the preferred format to the query string e.g. ?format=xml
  • 使用 HTTP Accept:标头
  • 将首选格式附加到查询字符串,例如?format=xml

See the Nothing but REST!web service example for how to develop a complete REST-ful Ajax CRUD app with only 1 page of jQueryand 1 page of C#.

没有什么,但休息!Web 服务示例,用于说明如何仅使用1 页 jQuery1 页 C#开发完整的 REST-ful Ajax CRUD 应用程序。

A good place to start is the Hello World exampleto see how to easily add ServiceStack web services to any existing ASP.NET web application.

一个很好的起点是Hello World 示例,以了解如何轻松地将 ServiceStack Web 服务添加到任何现有的 ASP.NET Web 应用程序。

Performance

表现

For the performance conscience, ServiceStack makes an excellent Ajax server as it comes bundled with the fastest JSON Serializer for .NET(> 3x faster than other JSON Serializers).

出于性能方面的考虑,ServiceStack 是一款出色的 Ajax 服务器,因为它捆绑了最快的 .NET JSON 序列化器(比其他 JSON 序列化器快 3 倍以上)。

Checkout this live Ajax appfor a taste (Live demo hosted on Linux/Nginx/MONO).

试试这个实时 Ajax 应用程序(在 Linux/Nginx/MONO 上托管的实时演示)。

Simple Northwind Example

简单的 Northwind 示例

ServiceStackalso makes it easy to create strong-typed frictionless web services where with just the code below is all you need to return a List of Customer POCOs:

ServiceStack还可以轻松创建强类型的无摩擦 Web 服务,只需使用以下代码即可返回客户 POCO 列表:

    public class CustomersService : RestServiceBase<Customers>
    {
            public IDbConnectionFactory DbFactory { get; set; }

            public override object OnGet(Customers request)
            {
              return new CustomersResponse { Customers = DbFactory.Exec(dbCmd =>
                    dbCmd.Select<Customer>()) 
            };
    }

With no other config, you can now call the above webservice REST-fully returning all of:

在没有其他配置的情况下,您现在可以调用上面的 webservice REST-完全返回所有:

Accessing web services on the client

访问客户端上的 Web 服务

You can call the above web service re-using the same DTOs that your web services were defined with (i.e. no code-gen is required) using your preferred generic ServiceClient (i.e Json, Xml, etc). This allows you to call your web services using a strong-typed API with just 1 Line of code:

您可以使用您的首选通用 ServiceClient(即 Json、Xml 等)重新使用定义您的 Web 服务的相同 DTO(即不需要代码生成)调用上述 Web 服务。这允许您使用强类型 API 调用您的 Web 服务,只需 1 行代码:

C# Sync Example

C# 同步示例

IServiceClient client = new JsonServiceClient("http://host/service");
var customers = client.Send<CustomersResponse>(new Customers());

And since your web services are also REST services, it works seamlessly with JavaScript ajax clients, e.g:

由于您的 Web 服务也是 REST 服务,因此它可以与 JavaScript ajax 客户端无缝协作,例如:

Using jQuery

使用 jQuery

$.getJSON("http://host/service", function(r) { alert(r.Customers.length); });

回答by gooseman

ASP.NET Web API is now the Microsoft framework for creating RESTful services.

ASP.NET Web API 现在是用于创建 RESTful 服务的 Microsoft 框架。

http://www.asp.net/web-api

http://www.asp.net/web-api