Simsons
Simsons

Reputation: 12745

Unable to access WebAPI using Controller and Action name using MVC5

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.

Upvotes: 0

Views: 534

Answers (2)

Shyju
Shyju

Reputation: 218732

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.

Upvotes: 2

redanesc
redanesc

Reputation: 360

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();
    }

Upvotes: 0

Related Questions