encodeuriccomponent不能与Web API调用一起工作

encodeURIComponent Not Working with Web API Call

本文关键字:调用 一起 工作 API Web 不能 encodeuriccomponent      更新时间:2023-09-26

我在打这个电话时得到了一个404 Not Found:

var url = rootWebApiUrl + '/folders/' + $scope.selectedServer.Name + "/" + serviceName;
$http.get(url) // the rest of this line doesn't matter for this issue

我认为也许使用encodeURIComponent会有所帮助,但我得到相同的404 Not Found错误:

var url = rootWebApiUrl + '/folders/' + $scope.selectedServer.Name + "/" + encodeURIComponent(serviceName);
这是Web API方法签名:
[Route("folders/{serverName}/{serviceName}")]
[HttpGet] 
public IEnumerable<Folder> Folders(string serverName, string serviceName)

如果在调试时更改服务名称,则上面的第一个选项有效。原服务名称为Company Name Message Bus Manager 3.6 - MesFinishingEvents。如果我将其更改为没有空格的东西,如snuh,则Web API调用成功,我可以在Web API方法中进行调试。

如何在$http.get(url)呼叫中传递原服务名称?

编辑:URL值:

使用带有空格的服务名:
http://localhost:4153/api/services/folders/ServerName/Company%20Name%20Message%20Bus%20Manager%203.6%20-%20MesFinishingEvents

使用简单的服务名称:
http://localhost:4153/api/services/folders/ServerName/snuh

当你的参数中有一个.时,WebAPI控制器会挣扎,这里和这里。

你的方法调用将一直工作到:

http://localhost:4153/api/services/folders/ServerName/Company%20Name%20Message%20Bus%20Manager%203

及以上,如果周期不存在

最快的解决方案是添加尾斜杠。较长的解决方案是修改IIS中的站点HTTP处理程序。