Solved

Routing in Web API

Posted on 2014-01-23
5
578 Views
Last Modified: 2014-01-24
Hi,

I have this scenario. I have a route like this:

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{search}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, search = UrlParameter.Optional }
            );

Open in new window


and I have a controller with 3 get's

        public DocResponse Get()
        {
            //some code
        }

        public DocResponse Get(string id, string search)
        {
            //some code
        }

        public PathResponse Get(string id)
        {
            //some code
        }

Open in new window


Now, if the url looks like this:

http://rest.smartdoc.se/api/documents

It runs the Get() function

If the url looks like this:

http://rest.smartdoc.se/api/documents/da46907f-c519-4d98-8279-6774afc651b4

it runs the Get(string id) function.

Now to my problem:

If the url looks like this:

http://rest.smartdoc.se/api/documents/fts/search%20string

I want to run the function Get(string id, string search) but it doesn't - it runs the Get(string id) function.

How can I force it to run like I want when two parameters are present?

Best regards
RTSol
0
Comment
Question by:RTSol
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39806335
My first guess would be that you need the "&" delimiter, since a %20 is HTML encoding for a space, which is not a parameter delimiter.

http://rest.smartdoc.se/api/documents/fts/search&string
0
 

Author Comment

by:RTSol
ID: 39806356
Hi,

Thats not it - it doesn't work for this either:

http://rest.smartdoc.se/api/documents/fts/searchstring

Best regards
RTSol
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39806750
I shouldn't answer questions when I tired...

The route is {controller}/{action}/{id}/{search}, so what is the controller, action, ID, and search?

I believe that you can use named arguments as well.

Example:

public ActionResult GetImages(string id, string apiKey)

   ...

/Artist/GetImages/cher?apiKey=XXX
0
 

Author Comment

by:RTSol
ID: 39807043
Hi,

I am not sure I understand. In my controller I have this:

public DocResponse Get(string id, string search)

which to me has two named arguments of type string.

This call has two string parameters

http://rest.smartdoc.se/api/documents/fts/searchstring

but in the controller this is run:

public DocResponse Get(string id)

which has only one argument. Is there a problem with the route?

Even if I change to this:

defaults: new { controller = "Home", action = "Index" }

this is run:

public DocResponse Get(string id)

I probably need to understand how to set up the route properly.

Best regards
RTSol
0
 

Author Closing Comment

by:RTSol
ID: 39807059
Hi again,

I tested again with this:

http://rest.smartdoc.se/api/documents/fts?search=searchstring

and it works fine.

Thanks a lot!

RTSol
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now