0

When refreshing the page in angular 2 application with ASP MVC, I get Server Error in '/' Application.

and when I open link in another tab I get the same error

Any help please ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ay Sy
  • 101
  • 1
  • 1
  • 7

2 Answers2

0

You should turn on logging to see what error is happening.

How to do that depends on your MVC version, but it seems you are not using .Net Core, so you can just turn off custom error mode.

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

This should show you the real error and you can figure out what's wrong further.

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
0

There are two options for solving this problem. You can either add the hash location strategy to your app module.

import { LocationStrategy, HashLocationStrategy } from '@angular/common';

@NgModule({
imports: [.... ],
declarations: [...],
bootstrap: [AppComponent],
providers: [
  {
      provide: LocationStrategy,
      useClass: HashLocationStrategy
  }
]
})
export class AppModule { }

This option will only work for the parts of your Angular2 app that live on the Home ASP Controller

Your second option is to add routes to your ASP Controller that match your Angular 2 app routes and return the "Index" View

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [ActionName("Angular-Route1")]
    public IActionResult AngularRoute1()
    {
        return View("Index");
    }

    public IActionResult Route2()
    {
        return View("Index");
    }
}
Allen Rufolo
  • 1,173
  • 1
  • 11
  • 14
  • Are there wild-cards you can use with this second method so that I wouldn't have to write out every route in the MVC application? Particularly where I have dynamic path names. – DoubleA Jul 21 '17 at 12:07