我正在尝试使用.net core和Angular 2设置路由,但是路由无法解析,因为它们是由服务器解析的。
我见过的一个解决方案是注册一条到您的家庭控制器或其他东西的默认路由...但是我没有任何MVC控制器。
我已将此添加到我的主要组件中(并完成了所有其他路由器先决条件)
@RouteConfig([
{ path: '/', name: 'Table', component: TableComp, useAsDefault: true },
{ path: '/login', name: 'Login', component: LoginComp }
])
我在startup.cs中确实有这些:
在ConfigureServices()中
services.AddMvc();
在Configure()中
app.UseMvc();
但是,由于我实际上并没有使用任何MVC控制器或注册任何MVC路由,因此我对如何在浏览器而不是服务器中解析角度路由以及为什么它们不只是执行导航操作一无所知事物...
以下配置应适合大多数在.NET Core中使用客户端路由的项目:
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("index.html");
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();
}
})
.UseCors("AllowAll")
.UseMvc()
.UseDefaultFiles(options)
.UseStaticFiles();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句