1

Hi I am just starting to mess with WebApi to setup a REST api for my angular app.

I simply just added a "Web API 2 Controller with read/write actions"

I then just try to test it by just typing the url in the browser but I get a 404.

Everything is pretty much out of the box.

WebApiConfig.cs looks like

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

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

RouteConfig.cs is

    public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Global.asax

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

        InitializeDatabase("Data Source=.;Initial Catalog=msproto;Integrated Security=True;MultipleActiveResultSets=True;");
    }

Is there something I have to add to the web config? If I add a normal controller "MVC 5 Controller with read/write actions" it will hit my breakpoints in the get requests.

4
  • 1
    What order are they being executed in the global.asax? Web API routes first or MVC routes first? Commented Oct 27, 2015 at 16:54
  • oh.. i actually dont even see the webapi config being called at all. How do I register that?(sorry im a js developer!) Commented Oct 27, 2015 at 16:59
  • I updated my post and added the global asax, I added GlobalConfiguration... but still not working Commented Oct 27, 2015 at 17:05
  • nm I took your comment and assumed I should probably move it to the top. looks like its working now! Commented Oct 27, 2015 at 17:07

1 Answer 1

2

Make sure the Web API routes are registered before the MVC routes in your Global.asax.

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

Comments

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.