Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8546921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:08:48+00:00 2026-06-11T13:08:48+00:00

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

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:08:49+00:00Added an answer on June 11, 2026 at 1:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET web service decorated with System.Web.Script.Services.ScriptService() so it can return json
In designing and learning about an ASP.NET Web API I've come across a few
I have an ASP.NET application. On some pages it requires a specific querystring to
Most of my ASP.NET website is accessible to an anonymous web users. However there
In Visual Studio, you can require that an assembly reference match a specific version
How can I require the Address element? If I perform an XSD validation on
I require is to have a single stored procedure which can execute and show
How can you use jQuery to include/require a different php file if window height
Can any user run inline assembly for current linux kernel or does that require
How can you determine which rubygem is being used in response to a require

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.