0

I have a problem.

WebApiConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}",
            defaults: new
            {
                id = RouteParameter.Optional,
                api = "api",
                namespaces = new string[] { "able.application.api" }
            }
        );
    }
}

Global.asax

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    GlobalConfiguration.Configuration.EnsureInitialized();
}

Controller

public class ContentModelController : ApiController
{
    public ContentModelController()
    {

    }

    [HttpPost, HttpGet]
    public HttpResponseMessage Get(int? id)
    {
        return null;
    }
}

The problem is: When I call get method, I get the following error.

CallUrl: http://localhost:54531/api/ContentModel/get?id=1

Please, help me.. :(

<Error> <Message> No HTTP resource was found that matches the request URI 'http://localhost:54531/api/ContentModel/get?id=1'. </Message> <MessageDetail> No type was found that matches the controller named 'ContentModel'. </MessageDetail> </Error>

3
  • Remove the defaults. They don't seem to apply to anything in the route. Commented Feb 11, 2017 at 21:51
  • Sorry.. I get the same error again. Commented Feb 12, 2017 at 6:57
  • are you using a local instance of IIS ? Commented Feb 12, 2017 at 14:13

2 Answers 2

1

use this template

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

instead of this

config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}",
        defaults: new
        {
            id = RouteParameter.Optional,
            api = "api",
            namespaces = new string[] { "able.application.api" }
        }
    );

in your WebApiConfig.cs file. Hope this will work for you.

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

Comments

1

Try following Saurabh Srivastava's suggestion and update your API call to: http://localhost:54531/api/ContentModel/get/1

Or you could update the method signature for Get to the following with the original call: public HttpResponseMessage Get([FromUri]int? id)

Comments

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.