1

I just recently added a new ApiController to my ASP.NET MVC4 project (I already had one ApiController also in the Controllers folder) called ServerInfoController. I created this using the CRUD template option; here's what it looks like:

using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebFrontend.Models;

namespace WebFrontend.Controllers
{
    public class ServerInfoController : ApiController
    {
        // GET api/serverinfo
        public Dictionary<string, string> Get()
        {
            var settings = GlobalStaticVars.StaticCore.LoadServerSettings("Last", "Server");

            if (settings != null)
            {
                return new Dictionary<string, string>
                                         {
                                             {"ServerIP", settings.Address},
                                             {"ServerPort", settings.Port.ToString()}

                                         };
            }
            return new Dictionary<string, string>
                                  {
                                      {"ServerIP", ""},
                                      {"ServerPort", ""}
                                  };
        }
    }
}

However, every time that I attempt to navigate to that resource in the browser, I receive a 404 error. I know that my routes are registered correctly as my other controller is still reachable at the /api endpoint.

The following is the route that is configured in WebApiConfig.cs (it's the default route):

public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "ActionApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
2
  • Can you show your routes for the web api? Also whats the url you are calling. Commented Mar 7, 2013 at 20:55
  • Done. I added it above. Also, the URL that I am calling would be something like localhost:66555/api/serverinfo Commented Mar 7, 2013 at 20:56

1 Answer 1

1

You don't need {action} part in the route. This is default routing:

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Gotcha; thanks! I had figured something like that out by setting the action part to RouteParameter.Optional.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.