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 8791995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:55:10+00:00 2026-06-13T22:55:10+00:00

In an ASP.NET Web API I have modified the default GET action to accept

  • 0

In an ASP.NET Web API I have modified the default GET action to accept an optional parameter which will declare the depth at which to get the Object:

    public ObjectModel Get(int id, int? loadType = 1)
    {
        if (loadType.Value == 1)
        {
            return GetDeepObjectModel(id);
        }
        else
        {
            return GetShallowObjectModel(id);
        }
    }

Given the default route:

    config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"\d+" }
        );

I have an issue with valid requests in that:

    https://www.mysite.com/api/objects/1234?loadType=1

will work, but I would also like this same action to work for the basic get – considering the loadType param as optional:

    https://www.mysite.com/api/objects/1234

With the second request, I get a 404 not found. It seems that the id is not getting counted as a matched variable when it is paired with an optional param.

Is there something I am missing here? I would very much not to start adding new routes to cover this issue, since my experience with MVC has told me that adding routes can get out of hand very quickly.

  • 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-13T22:55:11+00:00Added an answer on June 13, 2026 at 10:55 pm

    I can’t be entirely sure of this, but it seems the default value on my

        int? loadType = 1
    

    parameter is causing the issue. If I change the default value to null

        int? loadType = null
    

    And check in the action for setting the default value

        if (!loadType.HasValue)
        {
            loadType = 1;
        }
    

    both versions of the request are properly directed to the intended action.

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

Sidebar

Related Questions

I have the following action in my ASP.NET Web API: public IEnumerable<Car> carssOfUser(int id,
I have an asp.net web api application which is acting as a Relay to
Suppose I have a ASP.NET Web API controller that uses a service, which in
I have created a filter which inherits the System.Web.Http.Filters.ActionFilterAttribute in the asp.net web api
I have developed an ASP.NET Web API which response like this: <ArrayOfMyData> <MyData> <Id>1</Id>
I have a pretty standard asp.net web api set up with get post put
I have an ASP.NET MVC 4 application which uses the new ASP.NET Web API.
We currently have a classic ASP.NET web application which serves as our API. It
Since from version beta of ASP.NET Web API, I have used HttpActionContext.RequestContentKeyValueModel to get
I have an ASP.NET Web API (version 4) REST service where I need to

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.