如何使 angular 应用程序的基本 href / IIS 虚拟目录不区分大小写?
How to make base href / IIS virtual directory case insensitive for angular app?
有类似的 没有正确答案。
如果我在 app.module.ts 中设置:
providers: [
{ provide: APP_BASE_HREF, useValue: '/my/app'},
并输入 localhost:4200/my/app
this.location.path()
returns正确路径“”。
但是如果我输入 localhost:4200/my/App
this.location.path()
returns "/my/App" 这是错误的。
出于超出此问题的原因,我对 URL 进行了检查。所以我的问题是:如何使 base href 不区分大小写?
我假设 IIS 上的安装由于同样的原因而失败。
我认为最好的方法可能是使用重写规则强制 IIS 将用户重定向到 my/app
。但是我不懂IIS,所以没法给你解决办法。
如果你想在angular中做到这一点,你可以使用下面的代码。它通过工厂
动态提供 APP_BASE_HREF
令牌
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
我喜欢@David 的回答,但我发现我的 url 在部署时最终变成了 /my/app/my/app,我不知道为什么。如果 href 相同但大小写不同,我改变了他将用户重定向到正确的基本 href 的方法:
export function baseHrefFactory(): string {
const expectedBaseHref = '/my/app';
const currentBaseHref = window.location.pathname.substr(0, expectedBaseHref.length);
if (expectedBaseHref.toLowerCase() === currentBaseHref.toLowerCase() && expectedBaseHref !== currentBaseHref)
{
// The two base hrefs are the same, but not the same case.
// Redirect the user to the correct base href.
const newLocation = window.location.href.replace(currentBaseHref, expectedBaseHref);
window.location.replace(newLocation);
}
// this is the case for localhost (where we don't use a virtual directory)
return currentBaseHref;
}
@NgModule({
/**/
providers: [
{provide: APP_BASE_HREF, useFactory: baseHrefFactory}
]
})
export class AppModule
有类似的
如果我在 app.module.ts 中设置:
providers: [
{ provide: APP_BASE_HREF, useValue: '/my/app'},
并输入 localhost:4200/my/app
this.location.path()
returns正确路径“”。
但是如果我输入 localhost:4200/my/App
this.location.path()
returns "/my/App" 这是错误的。
出于超出此问题的原因,我对 URL 进行了检查。所以我的问题是:如何使 base href 不区分大小写?
我假设 IIS 上的安装由于同样的原因而失败。
我认为最好的方法可能是使用重写规则强制 IIS 将用户重定向到 my/app
。但是我不懂IIS,所以没法给你解决办法。
如果你想在angular中做到这一点,你可以使用下面的代码。它通过工厂
动态提供APP_BASE_HREF
令牌
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
我喜欢@David 的回答,但我发现我的 url 在部署时最终变成了 /my/app/my/app,我不知道为什么。如果 href 相同但大小写不同,我改变了他将用户重定向到正确的基本 href 的方法:
export function baseHrefFactory(): string {
const expectedBaseHref = '/my/app';
const currentBaseHref = window.location.pathname.substr(0, expectedBaseHref.length);
if (expectedBaseHref.toLowerCase() === currentBaseHref.toLowerCase() && expectedBaseHref !== currentBaseHref)
{
// The two base hrefs are the same, but not the same case.
// Redirect the user to the correct base href.
const newLocation = window.location.href.replace(currentBaseHref, expectedBaseHref);
window.location.replace(newLocation);
}
// this is the case for localhost (where we don't use a virtual directory)
return currentBaseHref;
}
@NgModule({
/**/
providers: [
{provide: APP_BASE_HREF, useFactory: baseHrefFactory}
]
})
export class AppModule