>>>>Hi,
>>>>
>>>>I've got my first Web Api working for data that uses integers as Ids. I'm now struggling to get it to work with a Guid.
>>>>
>>>>I make a call like this:
>>>>
>>>>
http://localhost:51673/api/routes/cb877aed-25f1-4a5a-9883-5814a3dec5fd
>>>>
>>>>but it is returning all routes. I set a break point in my controller and it is actually going into this code:
>>>>
>>>>
public IEnumerable<Route> Get()
>>>> {
>>>> return Uow.Routes.GetAll().OrderBy(r => r.rte_name);
>>>> }
>>>>
>>>>
>>>>not into this code:
>>>>
>>>>
public Route Get(Guid pk)
>>>> {
>>>> var route = Uow.Routes.GetById(pk);
>>>> if (route != null) return route;
>>>> throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
>>>> }
>>>>
>>>>My RouteConfig class is this:
>>>>
>>>>
public class RouteConfig
>>>> {
>>>> public static void RegisterRoutes(RouteCollection routes)
>>>> {
>>>> routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
>>>>
>>>> routes.MapRoute(
>>>> name: "Default",
>>>> url: "{controller}/{action}/{id}",
>>>> defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
>>>> );
>>>> }
>>>> }
>>>>
>>>>Do I have to make a change here to get it to work with Guids?
>>>
>>>I think the - makes the URL invalid. Did you try to encode the GUID?
>>
>>No, how do I do that?
>
>Url encoding for the guid is not necessary. The problem is with your parameter. Your route defines a parameter named "id". Your action in the controller has a parameter named "pk". If it doesn't match the name in the route, it won't be used as the route parameter. It could be used as a part of the query string though, so http://localhost:51673/api/routes/?pk=cb877aed-25f1-4a5a-9883-5814a3dec5fd should work.
Good catch!