2

I have added a Api Controller in my asp.net mvc5 web project, along with other controllers...i am contstantly getting resource not found error. please point out mistakes i might be making...

following is my Register method for WebApi routes

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

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

here is the Global.asax.cs

 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        //GlobalConfiguration.Configure(WebApiConfig.Register);

        WebApiConfig.Register(GlobalConfiguration.Configuration);
    }

here is my Api controller class

public class SearchController : ApiController
{
    public IEnumerable<string> GetSearch()
    {
        return new string[] { "value1", "value2" };
    }
}

"http://sukhdev.com/api/search" is the url scheme i use to call web api, but it persistenly "The resource cannot be found."

0

1 Answer 1

11

Why did you commented //GlobalConfiguration.Configure(WebApiConfig.Register);

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



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

5 Comments

GlobalConfiguration.Configure(WebApiConfig.Register) will register your routes
it is done by WebApiConfig.Register(GlobalConfiguration.Configuration);
no, but uncommenting GlobalConfiguration.Configure(WebApiConfig.Register) also not working
I've just checked order is important move GlobalConfiguration.Configure(WebApiConfig.Register); before RouteConfig.RegisterRoutes(RouteTable.Routes);
yups..i also came to this, i minutely observed the "Newly Created" MVC and Web API project by Visual Studio and observed the order in which the routes are regiesterd :) feeling Happy after hours :)

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.