WCF Rest 4.0中的简单URL路由,不带斜杠 [英] Simple URL routes in WCF Rest 4.0 without trailing slash

查看:111
本文介绍了WCF Rest 4.0中的简单URL路由,不带斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于WCF REST服务模板40(CS)的WCF REST 4.0项目.我想公开简单的服务端点URL 末尾不带斜杠.例如:

I have a WCF REST 4.0 project based on the the WCF REST Service Template 40(CS). I'd like to expose simple service endpoint URLs without trailing slashes. For example:

  1. CarService.cs
  1. CarService.cs
    • http://www.domain.com/cars - GET returns a list of all cars
    • http://www.domain.com/cars/123 - GET returns a single car with ID 123
  • http://www.domain.com/trucks - GET returns a list of all trucks
  • http://www.domain.com/trucks/456 - GET returns a single truck with ID 456

我将上述URL视为资源请求(而不是目录),这就是为什么我认为此处不适合使用斜杠的原因.

I look at the above URLs as resource requests (not directories), which is why I don't think trailing slashes are appropriate here.

不幸的是,我似乎无法获得想要的行为,因为我总是被重定向到/cars/和/trucks/ 并带有一个斜杠.

Unfortunately, I can't seem to get the behavior I want because I am always redirected to /cars/ and /trucks/ with a trailing slash.

这是我定义汽车"路线和服务方法的方式-请注意,我在任何路线或URI模板定义中均未包含任何斜杠:

Here's how I've defined the "cars" route and service method - note that I have not included any slashes in any of the route or URI template definitions:

// Global.asax.cs
RouteTable.Routes.Add(new ServiceRoute("cars", new WebServiceHostFactory(), typeof(CarService)));

// CarService.cs
[WebGet(UriTemplate = "")]
public List<Car> GetCollection()
{
    return DataContext.GetAllCars();
}

请注意,MVC无法以这种方式工作.使用MapRoute方法,我可以将请求直接路由到 http://www.domain.com/about ,而无需重定向到/about/.如何在WCF REST 4.0中获得相同的行为?

Note that MVC does not work this way. With the MapRoute method I can route requests directly to http://www.domain.com/about without a redirect to /about/. How can I get the same behavior in WCF REST 4.0?

推荐答案

您遇到的主要问题是当前版本的WCF REST导致307重定向(到"/),则WebGet属性中的UriTemplate为空字符串.据我所知,在当前版本中没有解决此问题的方法.

The primary issue that you're running into is that the current version of WCF REST causes a 307 redirect (to the "/") when you have an empty string for the UriTemplate in your WebGet attribute. As far as I know, there is no getting around this in the current version.

但是,鉴于您想要一个以下解决方案,有两种中间立场"解决方案:1)允许您区分服务,2)具有(相对)较短的URI.

However, there are a couple of "middle ground" solution to your problem given that you want a solution that 1) allows you to differentiate services, and 2) have (relatively) short URIs.

第一个解决方案 您可以将其放在您的global.asax文件中(每个

First Solution You can put this in your global.asax file (per this example). You can do a service route for each service:

RouteTable.Routes.Add(new ServiceRoute("cars", new WebServiceHostFactory(), typeof(CarService)));
RouteTable.Routes.Add(new ServiceRoute("trucks", new WebServiceHostFactory(), typeof(TruckService)));

此时,您可以在每个服务中填充UriTemplate:

At this point you can populate your UriTemplate in each service:

[WebGet(UriTemplate = "all")]
CarPool GetAllCars();

[WebGet(UriTemplate = "{carName}")]
Car GetCar(string carName);

这将允许您使用以下URI:

This will allow you URIs of:

www.domain.com/cars/all
www.domain.com/cars/123 or www.domain.com/cars/honda

与卡车类似:

www.domain.com/trucks/all
www.domain.com/trucks/123 or www.domain.com/trucks/ford

第二个解决方案 使用 REST入门工具包(即WebServiceHost2Factory)中的服务主机.

Second Solution Use the service host from the REST Starter Kit (i.e., the WebServiceHost2Factory).

RouteTable.Routes.Add(new ServiceRoute("cars", new WebServiceHost2Factory(), typeof(CarService)));

在使用上面尝试使用的URI时,这不会导致307重定向,因此可以为您提供所需的确切信息.尽管我意识到使用该服务主机工厂而不是WCF 4附带的工厂感觉有点奇怪.

This does not result in a 307 redirect when using the URIs that you're attempting to use above and thus, gives you exactly what you need. Though I realize that feels a little weird using that service host factory rather than the one that ships with WCF 4.

这篇关于WCF Rest 4.0中的简单URL路由,不带斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆