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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:34:58+00:00 2026-06-18T12:34:58+00:00

Im trying to create a custom API Controller method. I want to call it

  • 0

Im trying to create a custom API Controller method.
I want to call it by using /api/optionalgroup/getbypricelist/{id}
But cant get it to work.

This is my WebApiConfig

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "ApiByAction",
        routeTemplate: "api/{controller}/{action}",
        defaults: new { action = "Get", id = RouteParameter.Optional }
    );

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

    var json = config.Formatters.JsonFormatter;
    json.SerializerSettings.PreserveReferencesHandling =
        Newtonsoft.Json.PreserveReferencesHandling.Objects;

    config.Formatters.Remove(config.Formatters.XmlFormatter);
}

}

And this is my ApiController

    public class OptionalGroupController : ApiController
    {
        // GET api/optionalgroup
        public IEnumerable<string> GetByPricelist(int id)
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/optionalgroup/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/optionalgroup
        public void Post([FromBody]string value)
        {
        }

        // PUT api/optionalgroup/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/optionalgroup/5
        public void Delete(int id)
        {
        }
    }
}

But when i try to call it using uri /api/optionalgroup/getbypricelist/5 it doesnt work, but if i replace the GetByPricelist method with the default Get() it works with uri /api/optionalgroup

  • 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-18T12:34:59+00:00Added an answer on June 18, 2026 at 12:34 pm

    I found the solution.
    Just replaced the ApiConfig line
    routeTemplate: "api/{controller}/{action}",
    with
    routeTemplate: "api/{controller}/{action}/{id}"

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

Sidebar

Related Questions

I'm trying to code a progress bar...i'm using the youtube api to create custom
I'm trying to create a custom javascript class that inherits from google.maps.Map (V3 API).
I'm trying to create a custom usercontrol that acts like a button, but i
I'm trying to create a custom event with jquery 1.7, but I'm just learning
I am trying to use the ArcGIS 2.1 JS API to create a custom
I am trying to create custom array indexed from 1 using subscript operator. Getting
I'm trying to define a custom post action in a controller, but I'm having
I want to create a custom model binder in ASP.Net Web API. There are
Im trying to create a custom version of the RequiredAttribute to replace the built
I'm trying to create a custom TabItem that is dynamically added to a TabControl

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.