Cannot reach Action in api Web API 2 Controller in MVC 5 app

I’ve tried to follow default Web API tutorial: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

Here’s what I did:

  • Listing analytics for Goo.gl's url shortening API
  • Geolocation - how to get city from long and lat
  • Youtube API - Handling Videos that have Been Removed by Youtube
  • Windows Live API connexion issue
  • Postman form-data works, but the raw equivalent doesn't
  • Uncaught ReferenceError: checkLoginState is not defined using facebook API
  • 1) I added Action Routing in my WebApiConfig:

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

    2) I added a link on my nav bar with client side javascript call:

    <a onclick="RetrieveNext();" href="#">Retrieve next</a>
    

    3) Here’s my view:

    <div class="row">
        <div class="col-md-4">
            <h2>Next barcode</h2>
            <p id="barcode">
                No available barcode
            </p>
        </div>
    </div>
    
    <script>
        var uri = 'api/Barcode';
    
        $(document).ready(function () {
        });
    
        function RetrieveNext() {
            uri = 'api/Barcode/RetrieveNext';
            $.getJSON(uri)
                .done(function (data) {
                    $('#barcode').text(data);
                })
                .fail(function (jqXHR, textStatus, err) {
                    $('#barcode').text('Error: ' + err);
                });
        }
    </script>
    

    4) Here’s my simple ApiController with 1 Action:

    public class BarcodeController : ApiController
    {
        [HttpGet]
        public IHttpActionResult RetrieveNext()
        {
            string barcode = "123456";
            if (barcode == null)
            {
            return NotFound();
                }
            return Ok(barcode);
        }
    }
    

    When I click my link I’m getting: Error: Not Found inside of my <p id="barcode">, which means JavaScript works, but Action wasn’t called.

    Here’s Call details:

    enter image description here

    What I missed here? I put breakpoint in my Action and I can’t reach this code…

  • call multiple api urls and call at same time
  • Using extra callback parameters to Array.prototype.some
  • Getting the current location of a device using the on{X} api
  • How to use jQuery and Google Books API to load a viewer based on ID instead of ISBN
  • API - use GET to add, edit, and delete?
  • Facebook API - doesn't return all fields for photos
  • One Solution collect form web for “Cannot reach Action in api Web API 2 Controller in MVC 5 app”

    How stupid is THAT??? I found what is the issue here: 404 error after adding Web API to an existing MVC Web Application

    It’s related to global.asax file. Even when you add WEB API to your project, visual studio opens readme.txt file for you with few tips how to add few lines of code to your global.asax file. THERE’S no single word about WHERE you should put your code (here it’s really makes difference!)

    So, copying from post I listed ablove:
    While it doesn’t work with:

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

    It works with:

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