使用Asp.net 4.5进行Angular2路由 [英] Angular2 routing with Asp.net 4.5

查看:73
本文介绍了使用Asp.net 4.5进行Angular2路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助. 我开始使用Asp.net内核开发angular2,一切都变得非常完美. 为了将所有404请求重定向到index.html,我在Startup类中使用了这段代码.

I need a help. I start to develop angular2 with Asp.net core and everything was perfect. For redirecting all 404 request to index.html I use this code in Startup class.

app.Use(async (context, next) =>
        {
            await next();

            if (context.Response.StatusCode == 404
                && !Path.HasExtension(context.Request.Path.Value))
            {
                context.Request.Path = "/index.html";
                await next();
            }
        });

但是,我需要返回asp.net 4.5,现在路由存在很大的问题.我尝试在Owin Startup类中使用类似的代码,但这不能解决问题.

But, I need to return to asp.net 4.5 and I have now a big problem with routing. I try to use similar code in Owin Startup class but that didn't resolve a problem.

如何将所有请求移至index.html? 一个例子:我有重定向到/logout的链接,但是现在有了这段代码app.module看不到/logout,我被重定向到了app的主页.

How to move all request to index.html? One example: I have link which redirect to /logout, but with this code now app.module don't see /logout and I get redirected to home page of app.

在教程中,如何在ASP 4.5中使用angular2( https ://angular.io/docs/ts/latest/cookbook/visual-studio-2015.html ) 在最后一段中说:

In tutorial how to use angular2 with asp 4.5 (https://angular.io/docs/ts/latest/cookbook/visual-studio-2015.html) in last paragraph says:

如果此应用程序使用Angular路由器,则浏览器刷新可能 返回404-找不到页面.查看地址栏.可以 包含导航网址(深层链接").../以外的任何路径 /index.html?

If this application used the Angular router, a browser refresh could return a 404 - Page Not Found. Look at the address bar. Does it contain a navigation url (a "deep link") ... any path other than / or /index.html?

您必须将服务器配置为返回以下内容的index.html 要求.在执行此操作之前,请删除导航路径并再次刷新.

You'll have to configure the server to return index.html for these requests. Until you do, remove the navigation path and refresh again.

如何执行此操作? 谢谢!

How to do this? Thank you!

推荐答案

您可以将所有内容重定向到index.html页面以解决此问题,这是我在一个项目中使用的配置代码:

You could redirect everything to the index.html page to solve this issue, here's the configuration code I used in one of my project:

<rewrite>
  <rules>
    <rule name="RedirectEverything" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.html" />
    </rule>
  </rules>
</rewrite>

在web.config的system.websever部分下添加此

Add this under the the system.websever section in your web.config

这篇关于使用Asp.net 4.5进行Angular2路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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