如何让 JS 知道应用程序根? [英] How do I make JS know about the application root?

查看:21
本文介绍了如何让 JS 知道应用程序根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MVC 项目中有一些引用 /jobs/GetIndex 的 javascript.这在我本地运行时效果很好,因为我有一个名为 JobsControllerController.当我部署它时,我将应用程序部署到一个Jobs"子文件夹,这意味着 URL 应该变成 http://site/jobs/jobs/GetIndex 但它会变成 http://site/jobs/GetIndex,我推测是因为 javascript 不知道根 URL"是什么,并使用该站点作为根.我怎样才能让它在两种环境中都能工作?

I have some javascript that's referencing /jobs/GetIndex in an MVC project. This works great when I run it locally because I have a Controller called JobsController. When I deploy it, I deploy the application to a 'Jobs' subfolder, which means the URL should become http://site/jobs/jobs/GetIndex but it's going to http://site/jobs/GetIndex, I presume because the javascript doesn't know what the 'root URL' is, and uses the site as the root. How can I get it to work in both environments?

推荐答案

如果您只是关心获取站点的根/基本 url,以便您可以附加它以获取您想要的其他 url,您可以简单地使用 / 作为您网址的第一个字符.

If you simply care about getting the root/base url of the site so you can append that to get the other url you are after, you may simply use / as the first character of your url.

var urlToJobIndex2= "/jobs/GetIndex";

<小时>

这里是另一种方法,如果您想要的不仅仅是应用根目录(例如:特定网址(使用 mvc 辅助方法构建,例如 Url.RouteUrl 等)


Here is an alternate approach if you want more than just the app root (Ex : Specific urls( built using mvc helper methods such as Url.RouteUrl etc)

您可以在 razor 视图中使用 Url.Content 辅助方法来生成应用程序库的 url,将其分配给一个 javascript 变量并在您的其他 js 代码中使用它来构建您的另一个网址.这样做时始终确保使用 javascript 命名空间,以避免全局 javascript 变量可能出现的问题.

You may use the Url.Content helper method in your razor view to generate the url to the app base, assign it to a javascript variable and use that in your other js code to build your other urls. Always make sure to use javascript namespacing when doing so to avoid possible issues with global javascript variables.

如果您想获取特定操作方法的 url,您可以使用 Url.ActionUrl.RouteUrl 辅助方法.

If you want to get url to a specific action method, you may use the Url.Action or Url.RouteUrl helper methods.

因此在您的剃刀视图(布局文件或特定视图w)中,您可以这样做.

So in your razor view (Layout file or specific view), you may do this.

<script>
    var myApp = myApp || {};  
    myApp.Urls = myApp.Urls || {};
    myApp.Urls.baseUrl = '@Url.Content("~")';
    myApp.Urls.jobIndexUrl= '@Url.Action("GetIndex","jobs")';
</script>
<script src="~/Scripts/PageSpecificExternalJsFile.js"></script>

在你的 PageSpecificExternalJsFile.js 文件中,你可以像这样阅读它

And in your PageSpecificExternalJsFile.js file, you can read it like

var urlToJobIndex= myApp.Urls.jobIndexUrl;
// Or With the base url, you may safely add the remaining url route.
var urlToJobIndex2= myApp.Urls.baseUrl+"jobs/GetIndex";

这里是关于相同的详细答案,但在特定页面/视图中使用它

Here is a detailed answer about the same , but using this in a specific page/view

Angular Js

您的 angular 控制器/服务/指令中可能需要正确的相对 url.同样的方法也适用于您的 angular 代码.只需构建 js 命名空间对象并使用 angular value 提供程序将数据传递给您的 angular 控制器/服务/指令,如在这篇文章中详细介绍了示例代码.

You might need the correct relative url(s) in your angular controller / services / directives.The same approach will work for your angular code as well. Simply build the js namespace object and use the angular value provider to pass the data to your angular controllers/services/directives as explained in this post in detail with example code.

这篇关于如何让 JS 知道应用程序根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆