使用 mvc 刷新角度 2 中的页面时,我在应用程序中遇到服务器错误'/'?



在使用asp.net MVC的angular 2应用程序中刷新页面时,我得到服务器Error in '/' Application.

,当我在另一个选项卡中打开link时,我得到相同的错误

请帮忙

您应该打开日志记录以查看发生了什么错误。

如何做到这一点取决于你的MVC版本,但似乎你不使用。net Core,所以你可以关闭自定义错误模式。

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

这将显示真正的错误,您可以进一步找出错误所在。

解决这个问题有两个选择。你可以将哈希位置策略添加到你的应用模块中。

import { LocationStrategy, HashLocationStrategy } from '@angular/common';
@NgModule({
imports: [.... ],
declarations: [...],
bootstrap: [AppComponent],
providers: [
  {
      provide: LocationStrategy,
      useClass: HashLocationStrategy
  }
]
})
export class AppModule { }

这个选项只适用于你的Angular2应用中位于主ASP控制器上的部分

你的第二个选择是在你的ASP控制器中添加与Angular 2应用路由匹配的路由,并返回"Index"视图

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
    [ActionName("Angular-Route1")]
    public IActionResult AngularRoute1()
    {
        return View("Index");
    }
    public IActionResult Route2()
    {
        return View("Index");
    }
}

相关内容

最新更新