Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Web Api and Guids
Message
De
23/06/2014 11:45:19
 
 
À
23/06/2014 11:36:16
Information générale
Forum:
ASP.NET
Catégorie:
Web Services
Versions des environnements
Environment:
C# 4.0
OS:
Windows 8.1
Network:
Windows 2000 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01602225
Message ID:
01602236
Vues:
46
>>>>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.

Super! I just changed the parameter back to being "id" and that worked. That at least helps my understanding of how it finds the matching route to use.
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform