2
votes

So, this is a simple one. I have done this in the past. But, it seems that I am missing something.

WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    // Attribute routing.
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

Global.asax.cs

protected void Application_Start(object sender, EventArgs e)
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

SomeController.cs

[RoutePrefix("api")]
public class SomeController: ApiController
{
    [Route("some")]
    [HttpGet]
    private HttpResponseMessage GetSome(HttpRequestMessage request)
    {
        ...
    }
}

Now, when I call: http://localhost:54699/api/some, I get the following 404:

{ "Message": "No HTTP resource was found that matches the request URI 'http://localhost:54699/api/some'.", "MessageDetail": "No type was found that matches the controller named 'some'." }

Folder structure:

enter image description here

4

4 Answers

4
votes

Why is your controller method private this should be public

[RoutePrefix("api")]
public class SomeController: ApiController
{
    [Route("some")]
    [HttpGet]
    public HttpResponseMessage GetSome(HttpRequestMessage request)
    {
    ...
    }
}
0
votes

Try to move GlobalConfiguration.Configure(WebApiConfig.Register); after MVC's FilterConfig. This is a standard caveat with WebAPI attribute routing.

0
votes

you have /api in your routing configuration file.

you have mentioned /api as route prefix for controller and then action is "some".

the uri to "some" should be

http://localhost:<port>/api/api/some
0
votes
[RoutePrefix("api/some")]
public class SomeController: ApiController
{
    [HttpGet]
    public HttpResponseMessage GetSome(HttpRequestMessage request)
    {
    ...
    }
}

You method GetSome is private. Also, I'll modify the RoutePrefix attribute as in the code snippet to avoid rewriting the Route Attribute on each method.