【问题标题】:Tell .NET WebApi where Client-Routing starts告诉 .NET WebApi 客户端路由从哪里开始
【发布时间】:2017-05-17 10:48:59
【问题描述】:

我的应用包含 2 个参与应用。一个 asp.net-WebApi 和一个 angular2-clientsite-app。

我为我的 IIS 服务器上的 2 个应用程序创建了 2 个网站,但我遇到了路由问题。

这两个网站的网址是:

WebApi:http://testurl.com/api/

Angular2:http://testurl.com/ui/

现在在我的 anuglar2 应用程序中,我实现了一个典型的角度路由,它允许我访问例如这个链接: http://testurl.com/ui/search/result/562335

如果我导航到主站点,我可以轻松单击此链接,但如果外国用户复制该链接并与他的朋友分享,并且他们通过单击该链接开始访问我的站点,IIS 服务器会告诉他们,链接坏了……

如何编辑 web.config 文件以允许他们直接访问链接?

在此先感谢您的帮助!

【问题讨论】:

    标签: .net angular asp.net-web-api routing client-server


    【解决方案1】:

    我的问题有一个非常简单的解决方案。

    在您的路由文件“app.routing.ts”中,您只需将参数 useHash: true 添加到路由的 ExtraOptions 这是因为 IIS 需要段分隔符“#”来识别,直到他为网站提供服务。 #IIS知道后,剩下的URL就属于client-app了。现在看起来是这样的(注意最后一行代码):

       export const appRoutes: Routes = [
      { path: '', component: Search },
      { path: 'login', component: Login },
      { path: 'index.html', component: Search },
      { path: 'searchRooms', component: Search },
      { path: 'searchRooms/inquire', component: Search },
      { path: 'searchRooms/reserve', component: Search },
      { path: 'searchRooms/authCatcher', component: Search },
      { path: 'searchRooms/redirect', component: Search },
      { path: 'searchRooms/scheduler', component: Search },
      { path: 'searchRooms/scheduler/:roomId', component: Search },
      { path: 'inquire', component: Inquire },
      { path: 'register', component: Reserve },
      { path: '**', component: NotFound }
    ];
    
    export const appRoutingProviders: any[] = [
    
    ];
    
    export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes, {useHash: true});
    

    现在 angular 会自动更改所有路由:localhost:8080/search 变为 localhost:8080/#/search 因此 IIS 完全知道段分隔符 '#' 之后的路由不适合他并将其提供给客户端。

    还有另外两种解决这个问题的可能性:

    1. 在 web.config 文件中创建一个 url-rewriteRule。这里的问题是,您的浏览器只能从外部提供一个站点:第一个角度站点。如果你想从外部导航到特定的 Angular 站点,这是不可能的。

    2. 在 IIS 中更改错误页面 404(有点骇人听闻):在此解决方案中,您会告诉 IIS 他的错误页面是 Angular 应用程序。如果他无法提供提供给他的链接,他只需将 angular 称为错误页面,并且 angular 将接收 url 并可以路由到正确的站点。

    尽管我的解决方案已被证明是最好的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多