c# - Web API 2 - restful service - URL encoded -


i have created restful service using web api 2. have following route return information stock item :

http://localhost/api/stockitems/{stockcode}  i.e. http://localhost/api/stockitems/bomtest1 

i have stock codes in system contain forward slashes i.e. ca/base/sng/beech. naturally can't request details using standard convention due slashes.

http://localhost/api/stockitems/ca/base/sng/beech 

i have tried url encoding it's not hitting controller

http://localhost/api/stockitems/ca%2fbase%2fsng%2fbeech 

i keep getting 404

how handle in web api?

you need change webapiconfig. if don't use ids in more place can add wildcard ({*id}) in part of config:

config.routes.maphttproute(     name: "default",     routetemplate: "api/{controller}/{*id}",     defaults: new { id = routeparameter.optional } ); 

i'd recommend creating specific route case tho (assuming scenario need allow slashes):

config.routes.maphttproute(     name: "stockitems",     routetemplate: "api/stockitems/{*id}",     defaults: new { controller = "stockitems", id = routeparameter.optional } ); 

you won't need url encode urls way.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -