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:
What I missed here? I put breakpoint in my Action and I can't reach this code...