c# - window.location.href 将 HttpContext.Current.Request.Path 返回为“/”
问题描述
我有一个获取当前请求路径的过滤器。对于某些情况,我使用过
window.location.href =@Url.Action("Action","Controller").
通常HttpContext.Current.Request.Path
返回Controller/Action
,但如果window.location.href
它总是返回"/"
。我可以使用window.location.href
重定向以外的其他方法来检索正确的路径吗?
解决方案
window.location.href
需要一个 JavaScript 字符串,但您的代码会呈现无效的 JS,因为生成的 URLUrl.Action(...)
没有被撇号或引号包围。
正确的代码是
window.location.href = '@Url.Action("Action","Controller")';
但是,我推荐HttpUtility.JavaScriptStringEncode作为一个完全安全的解决方案:
window.location.href = @HttpUtility.JavaScriptStringEncode(Url.Action("Action","Controller"), addDoubleQuotes: true);
推荐阅读
- python - Matplotlib 实时数据更新图
- sql - 到 Azure SQL 数据库的数据流输出仅包含 Azure 数据工厂中的 NULL 数据
- scala - 获取模块符号,假设我有模块类,scala 宏
- php - MySQL 行限制和 AUTO_INCREMENT
- bitbucket - 如何使用 bitbucket 静态站点处理“不安全评估”
- javascript - R Shiny Datatable 中的嵌入式输入 - javascript 问题
- python - TypeError:“PosixPath”类型的参数不是 iterabl
- slice - 切片索引的惰性如何影响数组/列表的切片?[乐]
- c++ - C++:只允许数字作为输入
- angular - 部署后未显示数据表 [Netlify]