简体   繁体   English

Uri.EscapeDataString或HttpUtility.UrlEncode在我的情况下不起作用

[英]Uri.EscapeDataString or HttpUtility.UrlEncode does not work for my case

I have an API in a project which I coded as following: 我在一个项目中有一个API,我将其编码如下:

[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). 首先,当我从前端调用此API时遇到问题,如果客户包含点或空格(例如: https : //www.somesite.com/walmart Inc.),则会收到404错误(无法找到了此API)。 I find an easy way to solve this problem. 我找到一种解决此问题的简便方法。 Just add a "/" will solve this problem.( https://www.somesite.com/walmart Inc./ ) 只需添加“ /”即可解决此问题。( https://www.somesite.com/walmart Inc./)

Now I need to call this API in another project at the back end. 现在,我需要在后端的另一个项目中调用此API。 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. 我仍然收到404错误。 Then, I tried to use Uri.EscapeDataString or HttpUtility.UrlEncode to encode "name".( Does C# have an equivalent to JavaScript's encodeURIComponent()? ) 然后,我尝试使用Uri.EscapeDataString或HttpUtility.UrlEncode编码“名称”。( C#是否等效于JavaScript的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. 我仍然收到404错误。 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. 很抱歉输入错误,我删除了一些敏感信息,所以我误删除了“ api”。 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. 我已经测试了从后端的api调用是否可以在if名称仅包含字母或数字的情况下进行,但在name包含点时失败。

The problem is not relevant the customer parameter is encoded or not. 问题与customer参数是否编码无关。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM