首页 > 解决方案 > 查询字符串仅向控制器发送一半 URL

问题描述

我有一个 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&planid=a7f8&environment=dev&stack=dev

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

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

标签: javascriptasp.net-mvcasp.net-core.net-core

解决方案


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

encodeURIComponent(url)

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


推荐阅读