0

I have an WebAPI,

 public class SystemHealtController : ApiController
    {
        [HttpGet]
        public IHttpActionResult UpdateUsageDetail(string cpuUsage, string memoryUsage, string diskUsage)
        {

              // Do Stuff

            return Ok();
        }

RouteConfig also have default settings,

 public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Job", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

But when I navigate to API URL, http://localhost:64384/API/SystemHealt/UpdateUsageDetail?cpuUsage=2&memoryUsage=4&diskUsage=76 , get following error:

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

The only thing I suspect is, before creating Controller, I Installed

Microsoft.AspNet.WebApi 

and then uninstalled as well suspecting that could be the issue. But after uninstall also there were no errors.

Simsons
  • 12,295
  • 42
  • 153
  • 269
  • You have shown MVC routes, not web-api routes (refer [this article](https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api) for how to set them up) –  Nov 16 '17 at 01:31
  • The first code is Web API route and the second one is MVC route config. Show your API route (started with `public static void Register` in `WebApiConfig` file). – Tetsuya Yamamoto Nov 16 '17 at 01:33

2 Answers2

2

By default, the web api routing is registered in the WebApiConfig class with the route pattern api/{controller}/{id}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

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

You can see that there is no action method name. the methods are accessed by the verb (POST or GET, the type of call you are making). So typically you will create GET methods (to read data) and a POST method to post data to (for creating /updating data). When you want to use these endpoints, you will use the same url, but the Http method will be different (GET and POST)

So you should be accessing it like

yourSiteBaseUrl/api/SystemHealt?cpuUsage=2&memoryUsage=4&diskUsage=76

This should work, assuming you have Web api enabled in the app. If you are manually adding the web api functionality to an existing mvc project, Follow the steps mentioned in this post to do so.

By default, the route registration in RouteConfig is used for MVC controllers.

Shyju
  • 214,206
  • 104
  • 411
  • 497
0

have you tried putting a route prefix?

[RoutePrefix("api/SystemHealt")]
public class SystemHealtController : ApiController
{
    [HttpGet]
    public IHttpActionResult UpdateUsageDetail(string cpuUsage, string 
     memoryUsage, string diskUsage)
    {

          // Do Stuff

        return Ok();
    }
redanesc
  • 325
  • 4
  • 14