Solved

Routing in Web API

Posted on 2014-01-23
5
576 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 27
Maintain selection of the datagrdview 8 18
asp.net bundle 8 33
Problem to picture file 3 37
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.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

11 Experts available now in Live!

Get 1:1 Help Now