javascript - @Url.Content ("~") 不适用于本地主机

标签 javascript asp.net-mvc

<分区>

我正在尝试使用以下代码在 javascript 中解析应用程序的根 url:

 var rootUrl = '@Url.Content("~")';

但是上面的代码把rootUrl给成了/。 我应该怎么做才能将 url 设为 http://localhost:8000我的应用程序正在其上运行。

最佳答案

您可以直接从 JavaScript 获取它:

var rootUrl = window.location.href;

alert(rootUrl); 

The location property points to an object that contains information about the URL of the currently loaded page.

您将获得相同的结果:window.locationlocationlocation.href

阅读有关 window.location 的更多信息 here

关于javascript - @Url.Content ("~") 不适用于本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37184576/

相关文章:

javascript - 使用 jQuery Tabs 启用选项卡

c# - 为什么 aspx 在 if 语句中需要 {}

c# - 在数据表中呈现 HTML

c# - 忽略 New Relic 中的持久 SignalR 连接

javascript - 使用 React 引用嵌套 Javascript 数组的列值

javascript - 为什么 jquery 和 if 语句不能一起工作?

javascript - 通过 JQuery Ajax 从 http url 下载文件

javascript - 使用 jQuery 访问父元素的第一个/最后一个子元素中的数据属性

c# - SQL 语句的某些部分嵌套太深。重写查询或将其分解为更小的查询

asp.net-mvc - 为什么 ASP.NET 表单例份验证在静态图像请求上设置 cookie?