Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Razor 将管道短接在一起_Razor_Asp.net Core 2.0_Razorengine_Razor Pages - Fatal编程技术网

Razor 将管道短接在一起

Razor 将管道短接在一起,razor,asp.net-core-2.0,razorengine,razor-pages,Razor,Asp.net Core 2.0,Razorengine,Razor Pages,我希望在.NETCore2.1Razor页面中短路管道。具体地说,如果在不绑定模型或运行页面方法中的任何代码的情况下满足某个条件,我希望重定向到另一个页面。在下面的示例中,重定向只在页面方法中的所有内容完成后发生 public class TestModel : PageModel { public async Task<IActionResult> OnGetAsync() { //This line will still run after the

我希望在.NETCore2.1Razor页面中短路管道。具体地说,如果在不绑定模型或运行页面方法中的任何代码的情况下满足某个条件,我希望重定向到另一个页面。在下面的示例中,重定向只在页面方法中的所有内容完成后发生

public class TestModel : PageModel
{
    public async Task<IActionResult> OnGetAsync()
    {
        //This line will still run after the redirect called from within OnPageHandlerSelectionAsync.
        var test = 0;

        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        //This line will still run after the redirect called from within OnPageHandlerSelectionAsync.
        var test = 0;

        return Page();
    }

    public override async Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
    {
        if (true)//Some page specfic check, i.e. this will redirect to index page after 3PM.
        {
            //This line gets hit before OnGetAsnyc/OnPostAsync is called. 
            context.HttpContext.Response.Redirect("/Index");
        }
    }
}
公共类TestModel:PageModel
{
公共异步任务OnGetAsync()
{
//在OnPageHandlerSelectionAsync内调用重定向后,此行仍将运行。
var检验=0;
返回页();
}
公共异步任务OnPostAsync()
{
//在OnPageHandlerSelectionAsync内调用重定向后,此行仍将运行。
var检验=0;
返回页();
}
PageHandlerSelectionAsync(PageHandlerSelectedContext上下文)上的公共重写异步任务
{
if(true)//某些页面特定检查,即这将在下午3点后重定向到索引页面。
{
//在调用OnGetAsnyc/OnPostAsync之前,该行被命中。
context.HttpContext.Response.Redirect(“/Index”);
}
}
}
但它指的是管道短路/取消。不幸的是,MVC过滤器页面上的标题警告此页面的文档不适用于razor页面

当页面选择满足条件时,如何让页面放弃运行更多代码


*在声明中找不到该条件,因此无法应用自定义授权筛选器。

对于短路,您可以尝试
中间件
并根据您自己的逻辑检查请求,如下所示:

        app.Use(async (context,next) => {
            if (context.Request.Path.StartsWithSegments(new PathString("/Product")))
            {
                context.Response.Redirect("/Index");
            }
            await next();
        });
        app.UseMvc();

注意,在调用
app.UseMvc()之前使用
Middlware

不支持在Razor页面筛选器内短路或取消。您需要通过以下方式实现它


有关如何在资源筛选器中执行此操作,您可以执行以下操作。这在3.1版本中对我有效

public void OnPageHandlerExecuting(PageHandlerExecutingContext上下文)
{
如果(条件为真)
{                            
context.Result=新的重定向结果(yourpagename);
}
}