1

在我的 .net 核心项目中,我需要在部署时显示一个维护页面。所以,我决定编写一个动作过滤器并全局注册并设置配置值以检查何时维护,我将重定向到我的维护页面。问题是正在发生重定向,但网页不断重新加载。请找到下面的代码

MyActionFilterAttribute.cs

public class MyActionFilterAttribute: ActionFilterAttribute {
    private readonly IConfiguration _config;

    public OfflineActionFilterAttribute(IConfiguration config) {
        _config = config;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext) {
        if (filterContext == null) return;

        if (bool.Parse(_config.GetSection("Maintenance:Mode").Value) && filterContext.HttpContext ? .Request ? .Path.Value != "/Maintenance") {
            // Some logic goes here to satisfy conditions then only the below code should be executed.
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {
                controller = "Maintenance",
                action = "Index"
            }));

        }
        base.OnActionExecuting(filterContext);

    }
} 

启动.cs

在配置方法中,

services.AddMvc(options =>
         {
              options.Filters.Add(new OfflineActionFilterAttribute(_configuration));
         });

维护控制器

 public class MaintenanceController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }

网址 https://localhost:XXXXX/Maintenance 不断加载。

问题是:

  1. 如何停止重定向太多次问题。[请注意:我已经检查了其他堆栈溢出问题。它建议检查特定的控制器,这对我的情况没有帮助]。
  2. 有没有其他方法可以考虑?我有一个使用中间件的想法,我们可以发送响应,但我不确定如何重定向到包含动态内容的 .net razor 页面。

非常感谢任何帮助。

4

1 回答 1

0

根据您的描述,我建议您可以尝试使用自定义中间件。检查配置后,您可以直接设置响应上下文路径。

更多细节,您可以参考以下代码:

        app.Use(async (context, next) =>
        {
            if ("condition")
            {
                context.Response.Redirect("/Maintenance/Index");
                return;
            }
            else
            {   
                // Call the next delegate/middleware in the pipeline
                await next();
            }
           
   
        });

更新:

Startup.cs 配置方法:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseMigrationsEndPoint();
            app.UseRewriter(new RewriteOptions().AddRedirectToNonWww());

        }
        else
        {
            app.UseExceptionHandler("/Home/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.UseExceptionHandler("/Home/Error");
        app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod());
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();
        app.UseSession();   
        app.UseAuthentication();
        app.UseAuthorization();

        app.Use(async (context, next) =>
        {
            if (Condition && context.Request.Path != "/error/404")
            {
                context.Response.Redirect("/error/404");
                return;
            }
            else
            {                // Call the next delegate/middleware in the pipeline

                await next();
            }


        });


        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
         });
    }
于 2021-09-27T07:52:53.190 回答