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

The Archive Base Latest Questions

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

This is my first rodeo with MVC Web API and I’m having some issues

  • 0

This is my first rodeo with MVC Web API and I’m having some issues understanding the routing aspects. I would like to have a uri template similar to thise:

  1. http://google.com/api/AzureQueue – GET for all items in the queue
  2. http://google.com/api/AzureQueue/DeviceChart/ – GET returns devices and processing time for agent

http://google.com/api/{controller}/{id} <– default
http://google.com/api/{controller}/{chartType}/{id} where ID is optional

where I’m struggling is:
1. what the french toash do I put in the WebApiConfig.cs file
2. do I need to do anthing special in my controller eg. specifiy NonActions & Actions, Action Names, etc

Any help is appreciated

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

    You are almost there. The default route (in WebApiConfig.cs looks like this:

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

    There’s one very important caveat: the routes are examined in the order that they are declared with the first matching one being used, so the default route needs to go last.

    With that out of the way, you need to make a decision, do you want the calls for various chart types to go to one action, or many?

    For one action:

    WebApiConfig.cs

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

    AzureQueueController.cs

    public class AzureQueueController : ApiController
    {
        public string Get(string chartType)
        {
            return "chart = " + chartType;
        }
        public string Get(string chartType, int id)
        {
            return "chart = " + chartType + ",id = " + id.ToString(); 
        }
    }
    

    There are two things to notice here. In the anonymous class assigned to defaults, the value for controller decides which controller to route the request to. This can either be in the route template, or simply defined in the class. Also, a request of type Get is automatically sent to an action that starts with Get and has the arguments in the Url that match the template (there are two different cases since id is optional).

    This would be my preferred way to go unless the business logic for various charts is different.

    On the other hand you could specify this:

    WebApiConfig.cs

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

    Since I’m using the word action is the template, this will get interpreted as an action name.

    AzureQueueController.cs

        [HttpGet]
        public string DeviceChart()
        {
            return "chart = DeviceChart" ;
        }
    
        [HttpGet]
        public string DeviceChart(int id)
        {
            return "chart = DeviceChart" + ",id = " + id.ToString(); 
        }
    

    Here there is no string argument, that part of the url is being used to decide which action (public method) to use. Also, since the action names don’t start with Get, I need to add an attribute [HttpGet] for each method to mark them as being able to receive GET requests.

    Good luck with your project.

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

Sidebar

Related Questions

I have some issues with this: First I create my object and move it
I would like two threads work like this: First thread will append values to
I have some json code like this: { First name: David, Last name: Esseiva
i have two arrays like this first array Array ( [0228] => Array (
How to create in-app step by step instructions? Like this ( first boot android
Code first: '''this is main structure of my program''' from twisted.web import http from
I have string like this first#second, and I wonder how to get second part
Given an object like this: var obj = { first:{ second:{ third:'hi there' }
I am getting a json object like this { First: MyName's, Last: MyLast }
I have a psql script that looks like this: -- first set of statements

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.