Brett Brett - 1 year ago 115
ASP.NET (C#) Question

.NET MVC 4.5.2: Ignore certain routes and let Angular 2 handle them

I have a project built with Visual Studio 2015 and .NET MVC 4.5.2 and recently started using Angular 2 as the front-end. Before introducing Angular 2, I had routes such as /Plan/Dashboard and /Plan/Production. I used JQuery/Ajax to replace content when clicking links instead of doing full page refreshes, however the URL would never change, so clicking forward/back/refresh wouldn't work as you'd expect it to. Now that I am using Angular 2, the URL changes appropriately so back/forward buttons work, however refreshing the page doesn't work. For example, if Angular 2 routing changes my URL from /Plan/Production to /Plan/Production/Selling and I refresh it, I believe .NET searches for that route and can't find it, which results in the URL changing to

and the content simply displays "Error". My question is, is there a way to tell .NET to ignore any route that begins with /Plan/Production/ and allow Angular 2 to handle the routing for it? I will need to do this with other pages as well, but am focusing on /Plan/Production for now.

Answer Source

Try something like this in your web.config:

                <rule name="Redirect to NG2" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="Plan/Production/*" />
                    <action type="Rewrite" url="/Plan/Production" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

You will need to have the URL Rewrite Module installed to use this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download