1

I've recently asked a few questions about the best way to create a web api which utilises the same url as my main mvc site. I deduced the best way was to make the necessary changes to my MVC site to include web api and the necessary routing.

I have mainly followed How to add Web API to an existing ASP.NET MVC 4 Web Application project? but I have run into problems. The code compiles fine and it is clearly looking for the route but I get the error:

No HTTP resource was found that matches the request URI 'http://localhost:2242/api/value'. No type was found that matches the controller named 'value'.

My WebApiConfig:

class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional });
    }
}

my global.asax:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        Database.SetInitializer<ApplicationDbContext>(null);
    }
}

my api controller:

public class ValuesController1 : ApiController
{
    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/<controller>
    public void Post([FromBody]string value)
    {
    }

    // PUT api/<controller>/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/<controller>/5
    public void Delete(int id)
    {
    }
}

Other posts have corroborated that this is a correct and working setup...I created a separate webapi project to compare and this is all correct routing wise apparently. It would be far preferable to build this into my MVC website, does anyone have any ideas? This poster No type was found that matches controller had the same problem and the solution he found was to copy everything into a new project....that really isn't something I want to do/see why I should need to do.

Community
  • 1
  • 1
JonnyKnottsvill
  • 1,123
  • 2
  • 16
  • 39
  • 1
    The name of the controller (`ValuesController1`) doesn't match convention - in order to match `/api/value` the controller should be called `ValueController` – StuartLC Jul 09 '15 at 12:36
  • My god...I'm almost sorry for asking this question I didn't even notice the naming of it. Thanks....works fine – JonnyKnottsvill Jul 09 '15 at 12:40

2 Answers2

2

I think it is because of your Controller's name : ValuesController1

A controller has to be suffixed by "Controller", the 1 may be the cause of your issue.

Loot
  • 71
  • 8
1

The name of the controller ValuesController1 doesn't match convention - in order for the default route to match /api/value based on the default convention set in your call to configuration.Routes.MapHttpRoute(...), the controller should be called ValueController:

public class ValueController : ApiController
{
    public IEnumerable<string> Get()
    // ...

However, if you intend to deviate from the configured convention, you can apply RouteAttribute and RoutePrefixAttribute in conjunction with the Http* verb attributes to customise controller and method routes, e.g.

[RoutePrefix("api/Foo")]
public class ValuesController : ApiController
{
    // get api/Foo/value
    [HttpGet]
    [Route("value")] 
    public IEnumerable<string> NameDoesntMatter()
    {
        return new string[] { "value1", "value2" };
    }

    // get api/Foo/value/123
    [HttpGet]
    [Route("value/{id}")]
    public string AnotherRandomName(int id)
    {
        return "value";
    }

Before using the RouteAttribute you will need to add the following to your WebApiConfig.Register(HttpConfiguration config):

 config.MapHttpAttributeRoutes();

Even with the routing attributes, note however that the controller class name still needs to end with the suffix Controller, i.e. cannot end in the suffix 1. It is surprisingly difficult to alter this convention.

StuartLC
  • 104,537
  • 17
  • 209
  • 285