【问题标题】:ASP .Net 'Get' Web API Controller url with input parameters having # special character not working带有 # 特殊字符的输入参数的 ASP .Net“获取”Web API 控制器 URL 不起作用
【发布时间】:2021-10-03 22:49:40
【问题描述】:

让控制器接受用户名和密码参数。 如果密码有 # 特殊字符,则它不起作用,但它适用于其他特殊字符(!$@..)。 在邮递员中,# 被替换为 %23。

网址:https://localhost:8003/api/EmpInfoTest?userName=Afzal&userPwd=Afzal#123

public  class EmpInfoTestController : ApiController
    {
 public string Get(string userName, string userPwd)
        {

     //line1
     //line2
     //return
}
}

互联网上使用 Server.UrlEncode(url) 的解决方案。

让我知道如何以及在何处使用 urlEncode。

谢谢, 阿夫扎尔。

【问题讨论】:

  • 能否告诉我如何使用 UrlDecode 作为输入参数,谢谢。
  • 带有UrlDecode 的文档 - 这里是docs.microsoft.com/en-us/dotnet/api/…
  • 除了明显的安全问题外,UrlDecode 的问题在于你必须知道参数是 url 编码的,否则包含%23 的密码可能会被误认为#。更好的解决方案是将action方法改为POST,并将凭证作为表单提交提交,编码为application/x-www-form-urlencoded(即默认)
  • 如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…

标签: asp.net asp.net-core asp.net-web-api


【解决方案1】:

浏览器不会传递#字符。

通过测试,向服务器发送请求,发现后端方法中没有#参数和后面的字符。

所以结论是发送请求时应该处理urlencode。

我的示例代码(我使用 ajax):

我的测试结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多