Stuart Wells
Stuart Wells

Reputation: 43

How to set route path correctly in Asp.Net Web Api

http://xxxx/api/garmin/data throws the following error

No type was found that matches the controller named 'garmin'.

Below is the code in Global.asax

void Application_Start(object sender, EventArgs e) 
{
     RouteTable.Routes.MapHttpRoute(
        name: "ControllerAndAction",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional }
     );

     GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
     GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
}

Controller Code

public class GraminController : ApiController
{
    // GET
    [HttpGet]
    [ActionName("data")]
    public string GetGarminData()
    {
        return "value";
    }
}

The above code worked on the development server, but throws the below error

No HTTP resource was found that matches the request URI 'http://xxxx/api/garmin/data'. No type was found that matches the controller named 'garmin'.

What am I doing wrong? How can I fix the error?

Upvotes: 3

Views: 3125

Answers (1)

Mostafiz
Mostafiz

Reputation: 7352

Your controller name Gramin but you are trying to access it using garmin. Map you route this way

public class GraminController : ApiController
{
    // GET
    [HttpGet]
    [Route("api/garmin/data")]
    public string GetGarminData()
    {
        return "value";
    }
}

or if you want to use your way then make the call

http://xxxx/api/gramin/data

Upvotes: 4

Related Questions