Solved

Routing in Web API

Posted on 2014-01-23
5
589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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