Asp.net 核心 Blazor 服务器端 3.0 错误中的中间件重定向?
Middleware redirect in Asp.net core Blazor server-side 3.0 error?
我目前正在测试 asp.net core 3 blazor 服务器端应用程序。我在 f# 中构建了一个中间件扩展,并在 Startup class 的 Configure 方法中从 c# 调用它。它似乎最初尝试重定向,因为调用了正确的 url,但我收到一个错误页面,指出该页面未正确重定向。我在这里错过了什么。
F#:
type CheckMaintenanceStatusMiddleWare(next : RequestDelegate) =
let _next = next
member this.InvokeAsync(context : HttpContext) =
let statusCheck = true
if statusCheck
then
Task.Run(fun arg -> context.Response.Redirect("/Maintenance"))
else
_next.Invoke(context)
[<Extension>]
type CheckMaintenanceStatusMiddleWareExtensions() =
[<Extension>]
static member inline UseCheckMaintenanceStatus(builder : IApplicationBuilder) =
builder.UseMiddleware<CheckMaintenanceStatusMiddleWare>()
C#
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCheckMaintenanceStatus();
var connectionString = Configuration.GetConnectionString("DefaultConnection");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
//app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
剃刀组件:
@page "/Maintenance"
<h3>Maintenance</h3>
@code {
}
重定向循环可能发生如下:
(redirect-loop might happens)
Request Process Starts <---------------------------+
|-----> CheckMaintenanceStatusMiddleWare |
(check status) |
if fail then redirect to '/Maintenance' -------------->|
else
|----> go to inner middlewares by next(context)
为避免无休止的重定向循环,请检查当前路径是否已更改为/Maintenance
:
if statusCheck && context.Request.Path.Value <> "/Maintenance" then
... redirect
else
... invoke inner middlewares
我目前正在测试 asp.net core 3 blazor 服务器端应用程序。我在 f# 中构建了一个中间件扩展,并在 Startup class 的 Configure 方法中从 c# 调用它。它似乎最初尝试重定向,因为调用了正确的 url,但我收到一个错误页面,指出该页面未正确重定向。我在这里错过了什么。
F#:
type CheckMaintenanceStatusMiddleWare(next : RequestDelegate) =
let _next = next
member this.InvokeAsync(context : HttpContext) =
let statusCheck = true
if statusCheck
then
Task.Run(fun arg -> context.Response.Redirect("/Maintenance"))
else
_next.Invoke(context)
[<Extension>]
type CheckMaintenanceStatusMiddleWareExtensions() =
[<Extension>]
static member inline UseCheckMaintenanceStatus(builder : IApplicationBuilder) =
builder.UseMiddleware<CheckMaintenanceStatusMiddleWare>()
C#
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCheckMaintenanceStatus();
var connectionString = Configuration.GetConnectionString("DefaultConnection");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
//app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
剃刀组件:
@page "/Maintenance"
<h3>Maintenance</h3>
@code {
}
重定向循环可能发生如下:
(redirect-loop might happens)
Request Process Starts <---------------------------+
|-----> CheckMaintenanceStatusMiddleWare |
(check status) |
if fail then redirect to '/Maintenance' -------------->|
else
|----> go to inner middlewares by next(context)
为避免无休止的重定向循环,请检查当前路径是否已更改为/Maintenance
:
if statusCheck && context.Request.Path.Value <> "/Maintenance" then
... redirect
else
... invoke inner middlewares