【发布时间】:2018-08-09 20:55:50
【问题描述】:
我一直在将一个项目从 .NET Framework 迁移到 .NET Core 2,并且过程比预期的要顺利,但是当我认为我已经克服了最困难的挑战时,我终生无法获得SPA 方面的工作。
当我启动应用程序时,只要我转到根目录 (https://localhost:5001) 并且我可以很好地使用 Angular 路由进行导航,它就可以正常工作。但是,如果我尝试通过路由加载页面,例如。 https://localhost:5001/login,我得到一个通用的 Chrome 404“找不到这个本地主机页面”。
我的index.html 在目录MyApp/Pages/ 中,我的客户端代码在MyApp/wwwroot/app/ 中。将index.html 移动到wwwroot 文件夹时,我什么也得不到。
在我的Startup.cs 中,我是这样设置的:
public class Startup
{
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
var defaultFileOptions = new DefaultFilesOptions();
defaultFileOptions.DefaultFileNames.Clear();
defaultFileOptions.DefaultFileNames.Add("/index.html");
app.UseDefaultFiles(defaultFileOptions);
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 &&
!Path.HasExtension(context.Request.Path.Value))
{
context.Request.Path = "/index.html";
context.Response.StatusCode = 200;
await next();
}
});
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
}
我尝试了许多指南,但似乎我能找到的大多数指南都非常过时,或者在尝试使用帖子中的方法时,它们是我没有的方法。非常感谢任何帮助,因为我很想完成迁移并继续从事该项目。
【问题讨论】:
标签: c# angularjs asp.net-core asp.net-core-2.1 asp.net-core-mvc-2.0