0

I am using mvc5 web API to power a mobile app in xamrian, for the most part, the web API calls work 100% however today I came across a situation that where there was a full stop in the product code caused the call to the web API to fail.

But today I had a product code like this TFNT1116.5 and the API call failed.

public async Task<StockWarehouseInfoBins> GetWarehouseBinsInformaiton(string ItemCode, string WarehouseName)
{

        List<StockWarehouseInfoBins> _result = new List<StockWarehouseInfoBins>();

        var uri = new Uri(string.Format(BaseUrl + Constants.BinDetails + ItemCode.Trim(), string.Empty));

        var response = await _client.GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            var byteArray = await response.Content.ReadAsByteArrayAsync();

            var content = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
            _result = JsonConvert.DeserializeObject<List<StockWarehouseInfoBins>>(content);
        }

        return _result.FirstOrDefault();

}

My Client is a singleton of an HTTP client class.

HttpClient _client;

I think the problem is to do with the full stop in the product code.

This is the call that it gets to in my MVC controller.

[HttpGet]
// GET: Warhouses/Details/5
public ActionResult BinDetails(string id)
{

   List<StockWarehouseInfoBins> result = new List<StockWarehouseInfoBins>();

   result = database.GetWarehouseDetails(id);
   return Json(result, JsonRequestBehavior.AllowGet);
}

And this is my route information. When I test the call without product codes without full stops in them it works as expected and returns the correct JSON data.

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

This is my class here.

 public class StockWarehouseInfoBins
 {
    public StockWarehouseInfoBins();
    public long? BinItemID { get; set; }
    public string BinName { get; set; }
    public long? WarehouseItemID { get; set; }
    public long? WarehouseID { get; set; }
    public string WarehouseName { get; set; }
    public decimal ConfirmedQtyInStock { get; set; }
    public decimal QuantityReserved { get; set; }
    public decimal QuantityAllocatedBOM { get; set; }
    public decimal QuantityAllocatedSOP { get; set; }
    public decimal QuantityAllocatedStock { get; set; }
 }

I am calling the above in a async method. Which bininfo is getting null when the full stop is encourted.

 StockWarehouseInfoBins binInfo = new StockWarehouseInfoBins();
 binInfo = await restServices.GetWarehouseBinsInformaiton(lblstockCode.Text, SouceWarehouseName);

I presume I need to URL encode the API calls but is there a way to do this on the HTTP client so I don't need to change every call?

c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • 1
    i don't think full stops need to be encoded necessarily but it's the IIS server that might not handle the full stop the way you intend. Check this https://stackoverflow.com/questions/20998816/dot-character-in-mvc-web-api-2-for-request-such-as-api-people-staff-45287 – TheMikeInNYC Jul 11 '19 at 17:23
  • @TheMikeInNYC That was the reason once I put it in my web config it worked but just to be safe i turned it into query string as they are considered safe as well its for a phone app thats only internall so didnt matter if was a segment or a query string. – c-sharp-and-swiftui-devni Jul 13 '19 at 17:46

0 Answers0