ASP.NET Core Blazor 服务器端 3.0 中间件重定向错误?

3
我现在正在测试一个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 {

}

你的代码似乎一直让浏览器重定向? - itminus
1
如果是这种情况,请检查当前路径是否为/Maintenance,以避免重定向循环: if statusCheck && context.Request.Path.Value <> "/Maintenance" then - itminus
1
请注意,您不需要使用 let _next = next,可以直接使用 next。此外,在线程池上调度创建任务是昂贵的(由于各种原因),请考虑使用 Task.FromResult - CaringDev
@itminus,你的建议很有效。你能否将其作为答案提交,以便我接受它。此外,在你的答案中能否解释一下在这种情况下如何创建循环。 - user1206480
@CaringDev,您能否详细解释一下如何做到这一点。我在中间件构建的文档中没有看到任何示例,并且关于我的if语句,我只是试图与返回类型为Task的next.Invoke保持一致。 - user1206480
@user1206480 这更适合于CodeReview。请在那里发布一个问题(并在评论中提及我)。 - CaringDev
1个回答

3
重定向循环可能会出现如下情况:
                                              (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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接