3

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?

1
  • Yes, in Fiddler mentioned 'Content-Type: application/json; charset=utf-8' I am trying to make a simple GET request work, but that also fails. Commented Jul 25, 2016 at 9:10

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Mostafiz ..... I somehow missed that. Such a silly error. It's working now. Cause of simple spelling error, spent hrs trying to troubleshoot. Much appreciated.
Can I ask a question ... in the above code [Route ...] I am not able to define a [Route] attritube. Is there a namespace I need to import ?
System.Web.Http namespace, or directly System.Web.Http.Route()
Thanks Mostafiz, for some reason can't seem to import System.Web.Http.Route and the only import namespace I get is System.Web.Http.Routing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.