2

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:

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...

Bryuk
  • 3,295
  • 10
  • 45
  • 74
  • Try adding a value for `id` in the url -- does your action get called in that scenario? – David Tansey Dec 22 '14 at 17:53
  • have you tried `uri = 'api/Barcode';` It will probably disregard `RetrieveNext` and automatically grab the default `HttpGet` based on it's signature and give you what you want – scniro Dec 22 '14 at 17:57
  • I've tried add id and it didn't help. tried uri = 'api/Barcode'; and it doesn't work. This Controller will contain several [HttpGet] Actions, so I need to be able to reach exact action I need – Bryuk Dec 22 '14 at 18:03
  • what about camel casing the controller such as `uri = 'api/barcode'` and try your action name as well. In the tutorial about halfway down it shows the controller methods mapping to the URI and they are camel cased – scniro Dec 22 '14 at 18:07
  • I've tried that... Doesn't work – Bryuk Dec 22 '14 at 18:23

1 Answers1

7

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);
}
Community
  • 1
  • 1
Bryuk
  • 3,295
  • 10
  • 45
  • 74