如何使 Angular 应用程序的 base href/IIS 虚拟目录不区分大小写? [英] How to make base href / IIS virtual directory case insensitive for angular app?

查看:26
本文介绍了如何使 Angular 应用程序的 base href/IIS 虚拟目录不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有类似的问题没有正确答案.

There is similar question without proper answer.

如果我在 app.module.ts 中设置:

If I set in app.module.ts:

providers: [
  { provide: APP_BASE_HREF, useValue: '/my/app'},

然后输入 localhost:4200/my/app

and type localhost:4200/my/app

this.location.path()

返回正确的路径".

但是如果我输入 localhost:4200/my/App

However if I type localhost:4200/my/App

this.location.path()

返回错误的/my/App".

returns "/my/App" which is wrong.

出于这个问题之外的原因,我检查了 URL.所以我的问题是:如何使 base href 不区分大小写?

For reasons beyond this question I make checks for the URL. So my question is: how to make base href case insensitive?

我认为 IIS 上的安装由于同样的原因失败.

I assume that installation on IIS fails for same reason.

推荐答案

我认为最好的方法可能是使用重写规则强制 IIS 将用户重定向到 my/app.但是我不知道IIS,所以我不能给你一个解决方案.

I think the best way may be to force IIS to redirect the user to my/app by using rewrite rules. However I don't know IIS, so I cannot give you a solution.

如果你想用 angular 来做,你可以使用下面的代码.它通过工厂动态提供APP_BASE_HREF令牌

If you want to do it in angular, you could use the following code. It provides APP_BASE_HREF token dynamically with a factory

export function baseHrefFactory()
{
  let expectedBaseHref = '/my/app';
  let currentBaseHref = window.location.pathname.substr(0, expectedBaseHref.length);
  //Add checks herre if needed
  return currentBaseHref;
}

@NgModule({
/**/
  providers: [
    {provide: APP_BASE_HREF, useFactory: baseHrefFactory}
  ]
})
export class AppModule

这篇关于如何使 Angular 应用程序的 base href/IIS 虚拟目录不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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