In your target action method, just include args String param1, String param2 and String param3 and MVC will automatically wire them up to query strings.
If the query string name is different from the argument name, use the
FromUri attribute to do the mapping; see
here for an example.
public class RandomController : ApiController
{
public ZuneCrawler.WcfService.DTOs.Random Get(string category, string list, string offerType)
{
}
}
When I actually try to call the "Random" service in Fiddler, I get an HTTP 404 with this message: "<?xml version="1.0" encoding="utf-8"?><string>No action was found on the controller 'Random' that matches the request.</string>"
I thought that since the function is named "Get" it would automatically route correctly?
Call your method GetSomething rather than just "Get". First of all, do without the method args. When you have basic routing to an action sorted out, then add your args and check that MVC wires them up to "category", "list" and "offerType" query strings.
See
this article for a good overview of Web API routing.
My other 2 services that are defined in 2 other controllers work perfectly. Both those controllers have a single function called Get, with no parameters, and that works.
That article seems to show actions that are included in the URL
Ok I added a function like this:
public ZuneCrawler.WcfService.DTOs.Random Get()
{
return Get(null, null, null);
}
So now the service works if I call it in fiddler either with parameters or without.
However, even when putting parameters in the url, the parameters are still set to null when calling the function. So it's not routing it all the way correctly yet
Ok - stick with "Get" if that works. I thought it might have been a problem. One thing you can do is explicitly name the action in the defaults, in the same way that you name the controller.
In your target action method, just include args String param1, String param2 and String param3 and MVC will automatically wire them up to query strings.
If the query string name is different from the argument name, use the
FromUri attribute to do the mapping; see
here for an example.
Hi refter the aboce thread. it correct. it works similar as with .aspx pages
aemami99
Member
17 Points
13 Posts
How do you route optional parameters?
Feb 29, 2012 07:27 AM|LINK
I can't find any examples of how to make optional parameters. If I include a "?" in the routedTemplate, it throws an error
Just to clarify, I'm trying to make a service that has a url like this: /SomeServiceCall?param1=aasdf¶m2=blah¶m3=blahblah
kipo
All-Star
16475 Points
2811 Posts
Re: How do you route optional parameters?
Feb 29, 2012 07:39 AM|LINK
Not sure if this will help, but you can try: http://blogs.msdn.com/b/henrikn/archive/2012/02/23/using-asp-net-web-api-with-asp-net-web-forms.aspx
awebb
Member
204 Points
91 Posts
Re: How do you route optional parameters?
Feb 29, 2012 07:48 AM|LINK
In your target action method, just include args String param1, String param2 and String param3 and MVC will automatically wire them up to query strings.
If the query string name is different from the argument name, use the FromUri attribute to do the mapping; see here for an example.
aemami99
Member
17 Points
13 Posts
Re: How do you route optional parameters?
Feb 29, 2012 08:02 AM|LINK
It doesn't seem to work yet. This is how I have the routing set up:
config.Routes.MapHttpRoute( name: "default", routeTemplate: "{controller}", defaults: new { controller = "Random" });And this is the controller class:
public class RandomController : ApiController { public ZuneCrawler.WcfService.DTOs.Random Get(string category, string list, string offerType) { } }When I actually try to call the "Random" service in Fiddler, I get an HTTP 404 with this message: "<?xml version="1.0" encoding="utf-8"?><string>No action was found on the controller 'Random' that matches the request.</string>"
I thought that since the function is named "Get" it would automatically route correctly?
awebb
Member
204 Points
91 Posts
Re: How do you route optional parameters?
Feb 29, 2012 08:09 AM|LINK
Call your method GetSomething rather than just "Get". First of all, do without the method args. When you have basic routing to an action sorted out, then add your args and check that MVC wires them up to "category", "list" and "offerType" query strings.
See this article for a good overview of Web API routing.
aemami99
Member
17 Points
13 Posts
Re: How do you route optional parameters?
Feb 29, 2012 08:12 AM|LINK
My other 2 services that are defined in 2 other controllers work perfectly. Both those controllers have a single function called Get, with no parameters, and that works.
That article seems to show actions that are included in the URL
Ok I added a function like this:
public ZuneCrawler.WcfService.DTOs.Random Get() { return Get(null, null, null); }So now the service works if I call it in fiddler either with parameters or without.
However, even when putting parameters in the url, the parameters are still set to null when calling the function. So it's not routing it all the way correctly yet
awebb
Member
204 Points
91 Posts
Re: How do you route optional parameters?
Feb 29, 2012 08:20 AM|LINK
Ok - stick with "Get" if that works. I thought it might have been a problem. One thing you can do is explicitly name the action in the defaults, in the same way that you name the controller.
An example:-
routes.MapHttpRoute( name: "Eula", routeTemplate: httpRoutePrefix + "/eula", defaults: new { controller = "MyApi", action = "GetEula" } );vinay13mar
Star
7756 Points
1626 Posts
Re: How do you route optional parameters?
Feb 29, 2012 08:28 AM|LINK
Hi refter the aboce thread. it correct. it works similar as with .aspx pages
V.K.Singh
aemami99
Member
17 Points
13 Posts
Re: How do you route optional parameters?
Feb 29, 2012 08:30 AM|LINK
Still not working... tried naming the function "Get", and then setting the action to "Get".
Then tried naming the function "GetRandomApps" and set the action to "GetRandomApps".
Neither worked.
It's like the routing is being confused by having parameters...
aemami99
Member
17 Points
13 Posts
Re: How do you route optional parameters?
Feb 29, 2012 08:31 AM|LINK
I'm doing it the way awebb said to do it, and it's not working
I'm using self hosting and not IIS. Is that possibly causing a problem?