Andrey
Andrey

Reputation: 21285

Working with WebApi in a Web Forms application

I am trying to add WebApi in my Web Forms application (Visual Studio 2015, .NET 4.6). I added App_Start folder and WebApiConfig.cs in it as following (pretty much copied from an MVC app):

public class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        // Adding routes for WebApi controllers
        RouteTable.Routes.MapHttpRoute(
            name: "SearchApi",
            routeTemplate: "api/search",
            defaults: new { controller = "SearchController" }
            );

    }
}

Then, I created a folder Controllers/WebApi and added SearchController.cs:

namespace IdeaMaverick.Web.Controllers.WebApi
{
    public class SearchController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new [] { "value1", "value2" };
        }
    }
}

But when I hit http://example.com/api/search in the browser, I get this error:

{"message":"No HTTP resource was found that matches the request URI 'http://www.example.com/api/search'.","messageDetail":"No type was found that matches the controller named 'SearchController'."}

I'm obviously missing something but I can't figure out what.

Upvotes: 0

Views: 194

Answers (1)

Andrey
Andrey

Reputation: 21285

I found the issue - in defaults for the route I had to specify the controller name omitting "Controller", so it has to be like

defaults: new { controller = "Search" }

Upvotes: 0

Related Questions