...How does the route know to call the "PlacementsController" class? Is it literally just removing the word "Controller" from the class name?
Yes.
sitefinitysteve
2) How do I create my own Restful verbage? Like with Rest Preview I could design the Uri and map the querystring\other parts back to variables.
With WebApi, accepting URL paramaters and query string paramaters are both done with
model binding -- this means you can just accept paramaters in your action method and they are mapped from those two sources. If you want additional dynamic segments in the URL you do have to modify routing to indicate those.
Hmmm, okay...let me pick your brain on this then (routing related)
Here's my route to expose the API
RouteTable.Routes.MapHttpRoute(
name: "CpsPlacementsApi",
routeTemplate: "apps/api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
But I want to have different API "namespaces" for different parts of my app, like this
apps/api/cps/{controller}/{id}
apps/api/housing/{controller}/{id}
apps/api/karate/{controller}/{id}
...etc etc
How does one "constrain" the controllers to namespaces? Seems like right now it just looks into any controller in your project that inherits from ApiController regardless of namespace (correct?)
When I try and define a route like that it YSODs...so I guess I don't NEED to do take\skip with the oData bits though right?...but I still would like to define a querystring var because it seems more descriptive than
/api/placements/sites/1/2
The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
Parameter name: routeUrl
sitefinityst...
Member
7 Points
28 Posts
Couple things I don't get :/
Apr 27, 2012 06:23 PM|LINK
I'm used to the Rest Preview 2, and I'm a BIT lost on this...
1) How does the "Route" corrospond to the controller class? So like this code:
RouteTable.Routes.MapHttpRoute( name: "PlacementsApi", routeTemplate: "apps/api/{controller}/{id}", defaults: new { id = System.Web.Http.RouteParameter.Optional } );And this is the class
...How does the route know to call the "PlacementsController" class? Is it literally just removing the word "Controller" from the class name?
2) How do I create my own Restful verbage? Like with Rest Preview I could design the Uri and map the querystring\other parts back to variables.
Thanks,
Steve
BrockAllen
All-Star
27516 Points
4898 Posts
MVP
Re: Couple things I don't get :/
Apr 27, 2012 07:21 PM|LINK
Yes.
With WebApi, accepting URL paramaters and query string paramaters are both done with model binding -- this means you can just accept paramaters in your action method and they are mapped from those two sources. If you want additional dynamic segments in the URL you do have to modify routing to indicate those.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
sitefinityst...
Member
7 Points
28 Posts
Re: Couple things I don't get :/
Apr 27, 2012 07:27 PM|LINK
Hmmm, so gone are the days of simply specifying the Uri?
"/placement/{id}?take={take}&skip={skip}"?BrockAllen
All-Star
27516 Points
4898 Posts
MVP
Re: Couple things I don't get :/
Apr 27, 2012 07:30 PM|LINK
Routing is at a different position in the processing pipeline and it doesn't care about the query string, so I guess the answer is "yes".
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
sitefinityst...
Member
7 Points
28 Posts
Re: Couple things I don't get :/
Apr 27, 2012 07:34 PM|LINK
Hmmm, okay...let me pick your brain on this then (routing related)
Here's my route to expose the API
RouteTable.Routes.MapHttpRoute( name: "CpsPlacementsApi", routeTemplate: "apps/api/{controller}/{id}", defaults: new { id = System.Web.Http.RouteParameter.Optional } );But I want to have different API "namespaces" for different parts of my app, like this
apps/api/cps/{controller}/{id}apps/api/housing/{controller}/{id}apps/api/karate/{controller}/{id}...etc etc
How does one "constrain" the controllers to namespaces? Seems like right now it just looks into any controller in your project that inherits from ApiController regardless of namespace (correct?)
aliostad
Member
228 Points
55 Posts
Re: Couple things I don't get :/
Apr 27, 2012 07:39 PM|LINK
Routing is now a global concept. ASP.NET (MVC and Web API) assumes that you will use a global or controller level convention to define your routes.
Having said that, nothing stops you to define this route in your API:
config.Routes.MapHttpRoute( name: "mySpecialRoute", routeTemplate: "api/placement/{id}", defaults: new { id = RouteParameter.Optional, controller = "placement"And your controller having an action
public IEnumerable<MyType> Get(int id, [FromUri]int take, [FromUri]int skip) { ... }sitefinityst...
Member
7 Points
28 Posts
Re: Couple things I don't get :/
Apr 27, 2012 07:41 PM|LINK
OH! So generic controller ({controller}) isnt set in stone...I think I see...!
BrockAllen
All-Star
27516 Points
4898 Posts
MVP
Re: Couple things I don't get :/
Apr 27, 2012 08:01 PM|LINK
Yes, the {controller} parameter is required in the route either as part of the URL pattern or specified as a default paramater value.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
sitefinityst...
Member
7 Points
28 Posts
Re: Couple things I don't get :/
May 02, 2012 08:14 PM|LINK
When I try and define a route like that it YSODs...so I guess I don't NEED to do take\skip with the oData bits though right?...but I still would like to define a querystring var because it seems more descriptive than
/api/placements/sites/1/2
The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
Parameter name: routeUrl
aliostad
Member
228 Points
55 Posts
Re: Couple things I don't get :/
May 02, 2012 09:28 PM|LINK
Hi,
Yes that is correct, Web API does not allow it. You should use [FromUri] attribute.
I have updated my answer. I should have tested it :)