引用资源在全球的方式或者从一个虚拟目录或Web根? [英] Referencing resources in a global way either from a virtual directory or the web root?

查看:155
本文介绍了引用资源在全球的方式或者从一个虚拟目录或Web根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我在本地运行我有一个MVC /的​​WebAPI / AngularJS网站,例如

Let's say I have an MVC/WebAPI/AngularJS site that I'm running locally, e.g. ;

本地主机/测试/

然后我想要移动到

www.test.com

www.test.com

在当地,我有很多以下格式的各种目录(jsfiles等)引用(无论JS或HTML文件)

While local, I have a lot of references to various directories (jsfiles, etc) of the following format (in either JS or HTML files)

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:'='
        }
    }
})

我可以手动做到这一点(这是我一直在做什么),但该项目的增长越大,越难。我当然可以,只改变一次,然后在发布阶段(web.config中/其他)排除的文件,但它仍然感觉我会约了错误的方式。使用〜/不会在普通的HTML工作/ JS文件,据我所知,这个我真的不能使用它...

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. Using "~/" wouldn't work on plain HTML/JS files as far as I'm aware, and this I can't really use it...

任何建议映射到全球范围内的路径无论在虚拟目录或项目的根?

Any suggestions to map to paths globally regardless of whether in a Virtual Directory or the root of a project?

感谢:)

推荐答案

您应该不难code一样,您的应用程序基本路径。您可以使用 Url.Content Url.RouteUrl 在你的Razor视图生成的URL应用辅助方法基础。它会照顾构建正确的URL,无论你目前的网页/ path.Once你得到这个值,将其分配给一个JavaScript变量和使用,在你的其他的js code来构建其他URL。 始终确保这样做时,使用JavaScript命名空间,以避免与全局JavaScript变量可能存在的问题。

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. 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. Always make sure to use javascript namespacing when doing so to avoid possible issues with global javascript variables.

因此​​,在您的Razor视图(版式文件或特定视图),可以做到这一点。

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("~")';       
</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;

    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.

在您的非角Java脚本文件。

In your non angular java script files.

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

这篇关于引用资源在全球的方式或者从一个虚拟目录或Web根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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