简体   繁体   English

如何更改IIS默认网站的angularJS指令

[英]How to change angularJS directive for IIS Default Web Site

I am creating a web application with ASP.NET MVC and AngularJS. 我正在使用ASP.NET MVC和AngularJS创建一个Web应用程序。 When I debug the web application the URL is like this: 当我调试Web应用程序时,URL是这样的:

http://localhost/Home/Index#/Login

When I publish it to the IIS development server I use then I use the Default Web Site, so the url will be like this: 当我将它发布到我使用的IIS开发服务器时,我使用默认网站,所以url将是这样的:

http://ipaddress/MyApp/Home/Index#/Login

Because of this my directives do not work. 因此,我的指令不起作用。 They are set up like this: 它们设置如下:

        '/Login':
            {
                templateUrl: '/Account/Login',
                controller: 'AccountController'
            },

On the IIS development server this directive calls the route of the Web application: 在IIS开发服务器上,此指令调用Web应用程序的路由:

 http://ipaddress/Account/Login

which should be 应该是

http://ipaddress/MyApp/Account/Login

Is there a way to change the directives to work in both cases? 有没有办法在两种情况下改变指令?

I have tried to play with the "/" character in front of the templateUrl, but it does not seem to work (it gets localhost/Home/Account/Login instead of localhost/Account/Login) 我试图在templateUrl前面使用“/”字符,但它似乎不起作用(它获取localhost / Home / Account / Login而不是localhost / Account / Login)

Just ran across this looking for an answer to a similar problem related to hosting an Angular app in an IIS application below a site. 刚刚遇到这个寻找与在网站下面的IIS应用程序中托管Angular应用程序相关的类似问题的答案。 I was able to fix the TemplateUrl problem by just adding a . 我只需添加一个就可以修复TemplateUrl问题. before the / 之前/

   '/Login':
        {
            templateUrl: './Account/Login',
            controller: 'AccountController'
        },

There are 2 options I can provide you with: 我可以为您提供两种选择:

  1. Server config 服务器配置

    Don't use virtual directories to host your app in the dev environment. 不要使用虚拟目录在开发环境中托管您的应用程序。 Rather host different sites with different host headers in IIS so that you can refer to stuff from the root, for example: /Account/Login 而是在IIS中托管具有不同主机头的不同站点,以便您可以从根目录引用内容,例如: /Account/Login

  2. Configuration module 配置模块

    Consider using a module which gets injected into your angular app which contains a configuration you can use to build URLs for a specific environment. 考虑使用一个注入角度应用程序的模块,该模块包含可用于为特定环境构建URL的配置。 Then have one version of this module file for the dev environment, one for local and possibly one for production. 然后为开发环境提供此模块文件的一个版本,一个用于本地,一个用于生产。 The beauty of Javascript is that you can include different javascript files in the different environments, so the module could be sourced from different files. Javascript的优点在于您可以在不同的环境中包含不同的javascript文件,因此模块可以来自不同的文件。

    Have a look at this answer for more info on this option: https://stackoverflow.com/a/16340438/431522 有关此选项的更多信息,请查看此答案: https//stackoverflow.com/a/16340438/431522

Try to set the base url in master page header section like 尝试在主页面标题部分设置基本URL

<base href="/MyApp">

To differential between dev and release use conditional compilation. 对dev和release之间的差异使用条件编译。 See here C# and ASP.NET MVC: Using #if directive in a view 请参阅此处C#和ASP.NET MVC:在视图中使用#if指令

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

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