0

I have an API in a project which I coded as following:

[Route("api/SearchCustomer/{customer}")]
[HttpGet]
public List<Customer> SearchCustomer(string customer)
{
    return customerRepo.GetSearchJoined(customer);
}

At first, I got an issue when I am calling this API from my front end, if customer contains dot or space(for example: https://www.somesite.com/walmart Inc.), I will get 404 error(cannot found this API). I find an easy way to solve this problem. Just add a "/" will solve this problem.(https://www.somesite.com/walmart Inc./ )

Now I need to call this API in another project at the back end. So I did something like this:

var urlParm = "https://www.somesite.com/api/SearchCustomer/" + name + "/";
response = client.GetAsync(urlParm).Result;
var dataObjects = response.IsSuccessStatusCode ? response.Content.ReadAsAsync<IList<Customer>>().Result : null;
return dataObjects;

Unfortunately, adding the "/" at back does not work. I am still getting 404 error. Then, I tried to use Uri.EscapeDataString or HttpUtility.UrlEncode to encode "name".(Does C# have an equivalent to JavaScript's encodeURIComponent()?)

name = Uri.EscapeDataString(name)
or name = HttpUtility.UrlEncode(name)
or name = HttpUtility.UrlPathEncode(name)
var urlParm = "https://www.somesite.com/api/SearchCustomer/" + name + "/";
or var urlParm = = "https://www.somesite.com/api/SearchCustomer/" + name
response = client.GetAsync(urlParm).Result;
var dataObjects = response.IsSuccessStatusCode ? response.Content.ReadAsAsync<IList<Customer>>().Result : null;
return dataObjects;

I have tried all the different matches of above code. All of them did not work. I am still getting the 404 error. Does anyone know what I am doing wrong here?

Sorry for the typo, I removed some sensitive information so I deleted the "api" by mistake. The route is not the problem. I have tested that the api call from the back end worksif name contains only letters or numbers but fails when name contains dot.

user3293338
  • 481
  • 1
  • 6
  • 16
  • 2
    Shouldn´t it be [Route("api/SearchCustomer/{customer}")]??? – NicoRiff Jan 23 '18 at 16:55
  • 1
    "Then, I tried to use Uri.EscapeDataString or HttpUtility.UrlEncode to encode "name"." What was the output? – Scott Hannen Jan 23 '18 at 17:00
  • MVC routes don't care where the HTTP request comes from, only about the URL. If the local call fails, it means the URL is wrong - your route doesn't start with `api/`. – Panagiotis Kanavos Jan 23 '18 at 17:04
  • Sorry, that was a typo. I did have api in the code. I have tested, if the name is sth like "wal". The api works fine. – user3293338 Jan 23 '18 at 17:27
  • if you have a local environment, you should use localhost. Do you know for sure if what is published on your site contains that structure to consume the api? – makitocode Jan 23 '18 at 17:28
  • Try https://stackoverflow.com/questions/11494200/can-periods-be-used-in-asp-net-web-api-routes#answer-19870684 – Jared Lovin Jan 23 '18 at 17:52

1 Answers1

1

The problem is not relevant the customer parameter is encoded or not. You should specify the routing and apply the request correctly. Firstly fix the route;

[Route("api/SearchCustomer/{customer}")]

Then apply the request.

https://www.somesite.com/api/SearchCustomer/samplecustomer
lucky
  • 12,734
  • 4
  • 24
  • 46