Tuesday, January 28, 2014

Angluar JS + c# Web API decimals not routing

For some reason when i would put/get decimal values with the angularjs $resource i would get a 404. After lots of research i found out that this is due to $resource dropping trailing slashes (more info here: http://stackoverflow.com/questions/14533117/angular-trailing-slash-for-resource ). There is talk that in angular 1.3 they will add this as an option but for now we need to use a hack.
HACK: Add "\\/" to the end of your url in the $resource. For example: return $resource('/api/pricingAgreementItem/:id/:action/:value\\/', { id: '@id' },... See below for a more complete example.
 Javascript:
app.factory('PricingAgreementItemSvc', function ($resource) {
return $resource('/api/pricingAgreementItem/:id/:action/:value\\/', { id: '@id' },
{
// ...
setPrice: {
method: 'PUT',
params: {
id: '@id',
action: 'SetPrice',
value: '@value'
}
},
// ...
});
});
view raw gistfile1.js hosted with ❤ by GitHub
C#:
[HttpPut]
[Authorize(Roles = "VendorAdmin")]
[Route("{id:int}/SetPrice/{price:decimal}")]
public IHttpActionResult SetPrice(int id, decimal price)
{
//...
try
{
PricingAgreementItemService.SetPrice(id, price);
}
catch (Exception)
{
return NotFound();
}
return Ok();
}
view raw gistfile1.cs hosted with ❤ by GitHub

Phoenix

I am resurrecting this tech blog for notes related to Azure Logic Apps with SAP.