我现在正在测试一个asp.net core 3的Blazor服务器端应用程序。我使用F#构建了一个中间件扩展,并在Startup类的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 {
}
/Maintenance
,以避免重定向循环:if statusCheck && context.Request.Path.Value <> "/Maintenance" then
- itminuslet _next = next
,可以直接使用next
。此外,在线程池上调度创建任务是昂贵的(由于各种原因),请考虑使用Task.FromResult
。 - CaringDev