1

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();
 }
 });

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.

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.

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:

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?

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!

asked Dec 5, 2016 at 16:49

1 Answer 1

2

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>

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

bstoney
6,8245 gold badges46 silver badges51 bronze badges
answered Dec 5, 2016 at 16:55
Sign up to request clarification or add additional context in comments.

1 Comment

seconded...just found this as I have to use 4.5 with a Vue app, and had similar code as the OP. This worked awesome!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.