Solved

Routing in Web API

Posted on 2014-01-23
5
582 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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