Recently, I've been experimenting with using Web Api Asp.Net alongside Javascript to execute my controller. Here's an example of how I've been approaching this:
JS
function loadCatalog() {
apiService.get("../../api/CatalogoRegistro/get/", null,
function (res) {
$scope.Catalogos = res.data.Nombre;
},
errorCatalog);
}
Controller
[AllowAnonymous]
[HttpGet]
[Route("{catalog}")]
public HttpResponseMessage Fetch(HttpRequestMessage req, string catalog)
{
return CreateHttpResponse(req, () =>
{
HttpResponseMessage response = null;
var cr = _pService.List(param => param.Catalogos.Name.Equals(catalog));
IEnumerable<CatalogoRegistroViewModel> crs = Mapper.Map<IEnumerable<CatalogoRegistro>, IEnumerable<CatalogoRegistroViewModel>>(cr);
var version = req.Headers.GetValues("Version").FirstOrDefault();
List<string> errors = new List<string>();
if(cr==null)
{
errors.Add("The catalog does not exist");
}
if(errors.Count>0)
response = req.CreateResponse(HttpStatusCode.BadRequest, new { success = false, errors });
else
{
response = req.CreateResponse<IEnumerable<CatalogoRegistroViewModel>>(HttpStatusCode.OK, crs);
}
return response;
});
}
Angular View
<div class="page-head">
<div class="page-title">
<h1>
Generic Catalogs
<small>Manage Generic Catalogs of Applications</small>
</h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="portlet box blue">
... // (Content shortened for brevity)
</div>
</div>
</div>
However, I encountered an issue that states
GET http://localhost:55720/api/CatalogoRegistro/get/ 404 (Not Found)
I'm currently puzzled on why the routing for my api is causing this problem. Any insights would be greatly appreciated.