如何将window.baseUrl从剃刀代码返回到我的外部.js文件中

how to get back window.baseUrl from razor code into my external .js file

本文关键字:我的 外部 js 文件 返回 代码 window baseUrl 剃刀      更新时间:2023-09-26

我已经在视图页面的头部包含了这个javascript函数,我想在我的people.js文件中调用这个window.baseUrl,该文件位于script文件夹中:

   <head>
   <script type="text/javascript">
        window.baseUrl = '@Url.Content("~/")';  
   </script>
   @*<script type="text/javascript" src="@Url.Content("~/scripts/people.js")">    </script>*@
  </head>

在people.js文件中,我试图调用这样的函数:

self.followersUrl = window.baseUrl("/People/Followers?uid=" + data.UserId);

但它给了我一个错误:

Uncaught TypeError: window.baseUrl is not a function

我想知道如何注入这个。在查看页面上,我想发送以下url和其他类似的url:

 <a data-bind="attr: { href: followersUrl }">
  <a class="btn pull-right" data-bind="visible: !isFollowed && !isOwnProfile, attr: { href: followAction }">Follow</a>

根据Scott的回答,他建议这是因为window.baseUrl不是一个函数,所以我尝试了这个:

 self.followAction = window.baseUrl + '/People/Follow?uid=' + data.UserId;

现在,当我尝试点击跟随按钮时,

我在控制台中检查了它的给出错误:人/关注?uid=8 net::ERR_NAME_NOT_RESOLVED控制台中的url是people/Flow?uid=8:1,但我在浏览器选项卡中看到了正确的url,即people/Flow?uid=8

在这里,你可以看到大写的p变成了小p,然后我回到.js文件,尝试了这个:

 self.followAction = window.baseUrl + 'localhost:44305/People/Follow?uid=' + data.UserId;

然后它的发送url如下:

 https://localhost:44305/localhost:44305/People/Follow?uid=8

这里发生了什么。请给我推荐一些

window.baseUrl是字符串,而不是函数。

您可能想要:

window.baseUrl + '/People/Followers?uid=' + data.UserId;

我尝试了互联网上提供的一切,但都无法完成,然后我尝试手动插入数据,最后我写下了这行:

self.followAction = location.protocol + "//" + location.host + '/People/Follow?uid=' + data.UserId;

在这里,我不需要任何外部javascript函数,也不需要从任何地方调用它,它很神奇。