[英]Serving angular with routes in asp.net core 2 project
I have an angular project that I build with the cli "ng build --prod" and put in the wwwroot directory of my asp.net core project which I want to serve the site with. 我有一个用cli“ ng build --prod”构建的有角度的项目,并将其放在我希望与该网站一起使用的asp.net核心项目的wwwroot目录中。 The asp project has no MVC controllers I'm using so I don't have to take that into consideration.
asp项目没有我正在使用的MVC控制器,因此我不必考虑这一点。
I Setup the application to use default files and static files. 我将应用程序设置为使用默认文件和静态文件。 That gives me the angular application when I navigate to the server root, in this case, localhost:5000 => (delivers) index.html.
当我导航到服务器根目录时(在本例中为localhost:5000 =>(提供)index.html),这为我提供了有角度的应用程序。 From there I can use the application and routes work.
从那里我可以使用应用程序并路由工作。
But when I try to link to any route manually (eg. typing http://localhost:5000/quality-test-list ) I only get a white page with nothing. 但是,当我尝试手动链接到任何路由时(例如,键入http:// localhost:5000 / quality-test-list ),我只会得到一无所有的白页。
My guess is that the server side routing is prohibiting this. 我的猜测是服务器端路由禁止这样做。 I have looked at the standard "dotnet new angular" project files but I cannot figure it out.
我查看了标准的“ dotnet新角度”项目文件,但无法弄清楚。
My startup file: 我的启动文件:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// 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();
}
app.UseDefaultFiles();
app.UseStaticFiles();
}
}
in your configure section, use the app.use
method to configure all 404 to your index.html
在您的configure部分中,使用
app.use
方法将所有404配置为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();
}
})
.UseDefaultFiles(new DefaultFilesOptions { DefaultFilesName = new List<string>{ "index.html" } })
.UseStaticFiles(new StaticFilesOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDiretory(), "@wwwroot")),
RequestPath = new PathString("")
})
.UseMvc();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.