Skip to content

Commit

Permalink
VP-558 middlewar for giving index when 404
Browse files Browse the repository at this point in the history
  • Loading branch information
trueboroda committed Oct 1, 2019
1 parent ec1fe2d commit 39f648f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
41 changes: 41 additions & 0 deletions VirtoCommerce.Storefront/Middleware/SpaFallbackMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace VirtoCommerce.Storefront.Middleware
{
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class SpaFallbackMiddleware
{
private readonly RequestDelegate _next;

public SpaFallbackMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{

await _next.Invoke(context);

if (!context.Response.HasStarted && context.Response.StatusCode == (int)HttpStatusCode.NotFound)
{
//context.Response.Redirect("home/index");
context.Request.Path = "/";
await _next.Invoke(context);
}

}
}

// Extension method used to add the middleware to the HTTP request pipeline.
public static class SpaFallbackMiddlewareExtensions
{
public static IApplicationBuilder UseSpaFallbackMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<SpaFallbackMiddleware>();
}
}
}
16 changes: 11 additions & 5 deletions VirtoCommerce.Storefront/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddIdentity<User, Role>(options => { }).AddDefaultTokenProviders();

services.AddScoped<CustomCookieAuthenticationEvents>();
services.ConfigureApplicationCookie(options => {
services.ConfigureApplicationCookie(options =>
{
Configuration.GetSection("CookieAuthenticationOptions").Bind(options);
options.EventsType = typeof(CustomCookieAuthenticationEvents);
});
Expand Down Expand Up @@ -385,12 +386,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseMiddleware<NoLiquidThemeMiddleware>();
app.UseMiddleware<CreateStorefrontRolesMiddleware>();
app.UseMiddleware<ApiErrorHandlingMiddleware>();
// TODO: use only for SPA stores
app.UseSpaFallbackMiddleware();

//Do not use status code pages for Api requests
app.UseWhen(context => !context.Request.Path.IsApi(), appBuilder =>
{
appBuilder.UseStatusCodePagesWithReExecute("/error/{0}");
});
//app.UseWhen(context => !context.Request.Path.IsApi(), appBuilder =>
//{
// appBuilder.UseStatusCodePagesWithReExecute("/error/{0}");
//});

// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger(c => c.RouteTemplate = "docs/{documentName}/docs.json");
Expand Down Expand Up @@ -423,7 +426,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseMvc(routes =>
{
routes.MapSlugRoute("{*path}", defaults: new { controller = "Home", action = "Index" });
//routes.MapSpaFallbackRoute("angular-fallback",
// new { controller = "Home", action = "Index" });
});
//app.UseSpa(b => b.)
}
}
}

0 comments on commit 39f648f

Please sign in to comment.