3

How can I require a querystring for certain routes in an Asp.Net Web API?

Controllers:

public class AppleController : ApiController
{
    public string Get() { return "hello"; }
    public string GetString(string x) { return "hello " + x; }
}

public class BananaController : ApiController
{
    public string Get() { return "goodbye"; }
    public string GetInt(int y) { return "goodbye number " + y; }
}

Desired routes:

/apple        --> AppleController  --> Get()
/apple?x=foo  --> AppleController  --> Get(x)
/banana       --> BananaController --> Get()
/banana?y=123 --> BananaController --> Get(y)

3 Answers 3

3

Just do something like this:

public string Get(int y = -1)
{ 
    if(y < 0) return "goodbye"; 
    return "goodbye number " + y; 
}

That way it is one route, and covers all cases. You could factor each out as private methods as well for clarity.

Another method would be to add more routes, but since these are somewhat specific, you would have to add extra routes. For simplicity, I would say you change the methods GetString and GetInt to the same thing (like GetFromId so you can reuse a route:

routes.MapRoute(
    name: "GetFromIdRoutes",
    url: "{controller}/{id}",
    defaults: new { action = "GetFromId" }
);

routes.MapRoute(
    name: "GetRoutes",
    url: "{controller}",
    defaults: new { action = "Get" }
);

If you do not make these general enough, you could end up with a LOT of route entries. Another idea would be to put these into Areas to avoid route confilcts.

Sign up to request clarification or add additional context in comments.

3 Comments

I was hoping for something that felt less "hacky", but I guess this will have to do...
I will add another routed method too - stand by
Added a routed method as well.
0

You can specify a query string in your route as either optional or non (in Global.asax):

    ' MapRoute takes the following parameters, in order:
    ' (1) Pages
    ' (2) ID of page
    ' (3) Title of page
    routes.MapRoute( _
        "Pages", _
        "Pages/{id}/{title}", _
        New With {.controller = "Home", .action = "Pages", .id = UrlParameter.Optional, .title = UrlParameter.Optional} _
    )

This is VB.NET.

2 Comments

thanks but I'm looking for a way to do this with QueryString parameters
Can you not use something like Public Function LogOn(ByVal model As LogOnModel, ByVal returnUrl As String) As ActionResult on your actions?
0

I had a similar question this morning, and I think I found a simpler way to configure my routes. In your case, use this:

config.Routes.MapHttpRoute(
    name: "AppleRoute",
    routeTemplate: "apple",
    defaults: new { controller = "Apple" }
);

config.Routes.MapHttpRoute(
    name: "BananaRoute",
    routeTemplate: "banana",
    defaults: new { controller = "Banana" }
);

Just specify the controller, and let the framework select the correct action, based on whether your query string parameter is present or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.