简体   繁体   English

从虚拟目录或Web根以全局方式引用资源?

[英]Referencing resources in a global way either from a virtual directory or the web root?

Let's say I have an MVC/WebAPI/AngularJS site that I'm running locally, eg ; 假设我有一个本地运行的MVC / WebAPI / AngularJS网站,例如;

localhost/Test/ 本地主机/测试/

which I then want to move to 然后我想移至

www.test.com www.test.com

While local, I have a lot of references to various directories (jsfiles, etc) of the following format (in either JS or HTML files) 在本地时,我对以下格式(在JS或HTML文件中)的各种目录(jsfiles等)有很多引用

app.directive('rpdbSpinner', function() {
    return {
        restrict: 'E',
        **templateUrl: '/Test/templates/directives/spinner.html',**
        scope: {
            isLoading:'='
        }
    }
})

when updating/web publishing, I'd have to change everything to: 更新/网络发布时,我必须将所有内容更改为:

app.directive('rpdbSpinner', function() {
    return {
        restrict: 'E',
        **templateUrl: '/templates/directives/spinner.html',**
        scope: {
            isLoading:'='
        }
    }
})

I can do this manually (which is what I've been doing),but the larger the project grows, the harder it becomes. 我可以手动执行此操作(这是我一直在执行的操作),但是项目规模越大,难度就越大。 I could, of course, only change it once and then excluded the files during publishing phase (web.config/rest), but it still feels like I am going about it the wrong way. 当然,我可以只更改一次,然后在发布阶段(web.config / rest)排除文件,但仍然感觉像我在以错误的方式进行操作。 Using "~/" wouldn't work on plain HTML/JS files as far as I'm aware, and this I can't really use it... 据我所知,使用“〜/”不能在纯HTML / JS文件上使用,这我真的不能使用它...

Any suggestions to map to paths globally regardless of whether in a Virtual Directory or the root of a project? 是否有任何建议可以全局映射到路径,而不管是在虚拟目录中还是项目的根目录中?

Thanks :) 谢谢 :)

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. 如果您只是在乎获取站点的根目录/基础URL,以便可以附加该URL以获取您要查找的其他URL,则可以简单地使用/作为URL的第一个字符。

var getUsersUrl =  "/api/users";

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) 如果您不仅需要应用程序根目录, 这是一种替代方法 (例如:特定的urls(使用mvc帮助器方法构建,例如Url.RouteUrl等)

You should not hard code your app base path like that. 您不应该这样硬编码您的应用程序基本路径。 You may use the Url.Content or Url.RouteUrl helper methods in your razor view to generate the url to the app base. 您可以在剃刀视图中使用Url.ContentUrl.RouteUrl帮助器方法来生成指向应用库的URL。 It will take care of correctly building the url regardless of your current page/path.Once you get this value, assign it to a javascript variable and use that in your other js code to build your other urls. 无论当前页面/路径如何,它都将正确构建url。一旦获得此值,请将其分配给javascript变量并在其他js代码中使用它来构建其他url。 Always make sure to use javascript namespacing when doing so to avoid possible issues with global javascript variables. 一定要确保使用javascript命名空间,以避免全局javascript变量可能出现的问题。

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.userListUrl = '@Url.Action("Index","User")';       
</script>
<script src="~/Scripts/NonAngularJavaScript.js"></script>
<script src="~/Scripts/AngularControllerForPage.js"></script>
<script>
    var a = angular.module("app").value("appSettings", myApp);
</script>

In your angular controller, you can access it like, 在角度控制器中,您可以像这样访问它,

var app = angular.module("app", []);
var ctrl = function (appSettings) {

    var vm = this;
    console.log(appSettings.Urls.userListUrl);

    vm.baseUrl = appSettings.Urls.baseUrl;
    //build other urls using the base url now
    var getUsersUrl = vm.baseUrl + "api/users";
    console.log(getUsersUrl);

};
app.controller("ctrl", ctrl)

You can also access this in your data services, directives etc. 您也可以在数据服务,指令等中访问它。

In your non angular java script files. 在您的非角度Java脚本文件中。

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

Using "~/" wouldn't work on plain HTML/JS files as far as I'm aware, and this I can't really use it... 据我所知,使用“〜/”不能在纯HTML / JS文件上使用,这我真的不能使用它...

Yes, but you could inject it in your main server-side served webpage as a variable: 是的,但是您可以将其作为变量注入到主服务器端提供的网页中:

<script>
    var baseUrl = ... get the base url from the server using ~/
</script>

and then in your external scripts simply concatenate the relative urls with it. 然后在您的外部脚本中,只需将相对网址与其连接即可。 As far as static html files are concerned, then it could be a little more problematic. 就静态html文件而言,这可能会有更多问题。 You could serve them through some special server side handler that will take care of injecting this logic. 您可以通过一些特殊的服务器端处理程序为它们提供服务,这些处理程序将负责注入此逻辑。

You can use module.constant to create an injectable which you can use. 您可以使用module.constant创建可以使用的注射剂。

app.constant("URL_BASE", "/Test");

app.directive('rpdbSpinner', function(URL_BASE) {
    return {
        restrict: 'E',
        **templateUrl: URL_BASE + '/templates/directives/spinner.html',**
        scope: {
            isLoading:'='
        }
    }
})  

You can also use module.value if you register it before you register your directive. 如果在注册指令之前注册它,也可以使用module.value

For more information see AngularJS Module Guide -- configuration . 有关更多信息,请参阅《 AngularJS模块指南-配置》

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM