【问题标题】:Query string sending only half URL to controller查询字符串仅向控制器发送一半 URL
【发布时间】:2021-12-05 02:41:18
【问题描述】:

我有一个 URL,我试图将其作为查询字符串传递给控制器​​。但是当我这样做时,我只得到一半的 URL。 但是,console.log 会打印整个 URL。

export async function sendNotification(PlanId: string, URL: string) {
    console.log("Service URL : - ", URL);
    let url = HttpClient.buildUrl(`api/smsNotification/${PlanId}?URL=${URL}`);
    return await HttpClient.get(url);
}

[HttpGet("Notification/{PlanId}")]
        [ValidateModelState]
        public async Task<IActionResult> SendNotification(Guid PlanId, string URL)
        {
                return NoContent();
        }
控制台日志打印此 URL

https://chats.landbot.io/v3/index.html?name=Sys+Admin&amp;planid=a7f8&amp;environment=dev&amp;stack=dev

但在控制器,我得到了这个 URL

https://chats.landbot.io/v3/index.html?name=Sys Admin

【问题讨论】:

    标签: javascript asp.net-mvc asp.net-core .net-core


    【解决方案1】:

    查询字符串参数由&amp; 符号分隔。如您所见,这正是您的 URL 被截断的地方。为了让控制器获得完整的 URL,您需要在作为查询参数传递之前对其进行编码:

    encodeURIComponent(url)
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

    【讨论】:

      猜你喜欢
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2018-12-12
      相关资源
      最近更新 更多