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:
- 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
- TruckService.cs
- 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
I look at the above URLs as resource requests (not directories), which is why I don't think trailing slashes are appropriate here.
Unfortunately, I can't seem to get the behavior I want because I am always redirected to /cars/ and /trucks/ with a trailing slash.
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();
}
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?
You need a UriTemplate, Try something like this: