【问题标题】:How to choose the razor page to handle a request based on a cookie?如何选择剃须刀页面来处理基于cookie的请求?
【发布时间】:2020-06-22 19:44:36
【问题描述】:

我有一个 .NET Core 3.1 Razor Pages Web 应用程序,有 2 个页面; pageA.cshtml 和 pageB.cshtml。

在访问特定 URL 的站点时,我想为每个请求选择这两个页面中的哪一个应根据 cookie 的值来处理请求。
如果 cookie 是“A”,则使用 pageA.cshtml,如果是“B”,则使用 pageB.cshtml。

它不能是重定向。

我尝试查看中间件是否可以解决问题,但我找不到任何文档或示例来说明如何选择应处理请求的剃须刀页面。

【问题讨论】:

    标签: asp.net-core razor .net-core


    【解决方案1】:

    根据您的描述,我建议您可以编写自定义中间件来实现您的要求。

    您可以检查自定义中间件中的cookie并修改请求路径。

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

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        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.Use( async (context , next) => {
            if (context.Request.Path.Value.Contains("/page"))
            {
                if (context.Request.Cookies["Page"] != null)
                {
                    switch (context.Request.Cookies["Page"])
                    {
    
                        case "A":
                            context.Request.Path = "/pageA";
                            break;
                        case "B":
                            context.Request.Path = "/pageB";
                            break;
                        default:
                            break;
    
                    }
                }
            }
    
            await next();
        } );
    
        app.UseHttpsRedirection();
        app.UseStaticFiles();
    
        app.UseRouting();
    
        app.UseAuthorization();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }
    

    添加 cookie 页面代码:

        <form method="post" asp-page-handler="AddCookieA">
            <button>AddCookieA</button>
        </form>
    
        <form method="post" asp-page-handler="AddCookieB">
            <button>AddCookieB</button>
        </form>
    

    代码隐藏:

        public void OnPostAddCookieA() {
    
            Response.Cookies.Append("Page", "A");
       
    
        }
    
    
        public void OnPostAddCookieB()
        {
    
            Response.Cookies.Append("Page", "B");
    
    
        }
    

    结果:

    【讨论】:

    • 可以正常工作的完整代码示例,甚至还有一段视频展示了它的实际效果。我如何多次投票? :D
    【解决方案2】:

    在您的控制器操作中尝试以下操作:

    public ActionResult Get() 
    {
        if (Request.Cookies["COOKIE_1"] != null) {
            return View("View_Cookie1"); // Consider you have a View_Cookie1.cshtml file
        } else if (Request.Cookies["COOKIE_2"] != null) {
            return View("View_Cookie2"); // returns View_Cookie2.cshtml
        }
    }
    

    【讨论】:

    • 这看起来更像 MVC 而不是 Razor Pages,所以这种方法对我不起作用。
    猜你喜欢
    • 2019-03-18
    • 2018-09-14
    • 1970-01-01
    • 2019-06-25
    • 2020-11-06
    • 1970-01-01
    • 2019-08-13
    • 2012-08-03
    • 1970-01-01
    相关资源
    最近更新 更多