0

I have a controller in this folder structure:

Site
-Controllers
--API
---EventsController.cs

The EventsController.cs contains this:

[RoutePrefix("api")]
public class EventsController : Controller
{
    [Route("event")]
    public ActionResult Index()
    {
        return View("~/Views/Home/Index.cshtml");
    }

The WebApiConfig.cs contains this:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    // Web API routes
    config.MapHttpAttributeRoutes();

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

When I run the site from Visual Studio and try to access http://127.0.0.1:8080/api/event I see nothing but this error:

<Error>
  <Message>
    No HTTP resource was found that matches the request URI 'http://127.0.0.1:8080/api/event'.
  </Message>
  <MessageDetail>
    No type was found that matches the controller named 'event'.
  </MessageDetail>
</Error>

If I comment out the config.Routes.MapHttpRoute line to make WebApiConfig.cs as the following, the URL above works:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    // Web API routes
    config.MapHttpAttributeRoutes();

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

What am I doing wrong? What is it that causes the attribute routing to fail when the DefaultApi route is configured? I have tried placing it before/after the config.MapHttpAttributeRoutes(); and neither works.

As an aside, I have manually built up this project while reading the following article, which has the same structure of MVC/Web API project and which does work. I just can't figure out what I've done differently.

http://www.codemag.com/Article/1605081

3
  • 1
    It's not immediately obvious, but something that does stick out is your EventsController it looks like it inherits an Mvc controller and not an Api one. Also, What is the intent of EventsController? Is it an Mvc controller returning a view but with an /api/ route? My guess is the attribute routing is getting muddled because it's on an Mvc controller. Commented Jan 19, 2017 at 8:45
  • Thank you! Just 3 characters out of place and it all goes cray cray! Commented Jan 19, 2017 at 15:23
  • No problem, glad it's working. Commented Jan 19, 2017 at 19:17

1 Answer 1

1

Thanks to @phil-cooper the answer was to inherit the correct base class in the api controller.

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

2 Comments

So how how did you inherit correct base class. I’m having the same issue but not sure which 3 characters you removed to fix this. How did you fix it?
Ah, by "correct" I mean the EventsController from the Api namespace, not the Mvc namespace. I will try to update my posted answer with the correct line later, but I also haven't posted the using statements in the question, either. (I see that a lot here.) In short, the answer is to inherit from this namespace: learn.microsoft.com/en-us/dotnet/api/…

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.