ServiceStack 全局请求过滤器未触发 [英] ServiceStack Global Request Filter Not Firing

查看:42
本文介绍了ServiceStack 全局请求过滤器未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 mythz (ServiceStack dev) 建议的用于身份验证的全局请求过滤器,在此 SO 答案

I have a global request filter for authentication as suggested by mythz (ServiceStack dev), in this SO Answer

我的过滤器:

RequestFilters.Add((httpReq, httpResp, requestDto) =>
{
    if (!PublicRoutes.Contains(httpReq.PathInfo))
    {
        new AuthenticateAttribute().Execute(httpReq, httpResp, requestDto);
    }
});

当我请求继承 动态ViewPage

示例/Default.cshtml:

Example /Default.cshtml:

@inherits ViewPage
<!DOCTYPE html>
<html>
    <head>
        <title>HOME</title>
...
...
ETC

答案,在评论中,问题提出者提出了类似的行为,但没有准确描述如何重现,所以我看不到解决方案.

Down the bottom of the answer, in the comments, the question raiser suggests similar behaviour, but does not accurately describe how to reproduce, so I cannot see a solution.

有解决办法吗?我做错了什么吗?

Is there a solution? Did I do something incorrect?

更新

我发现我可以直接在我的页面上声明属性:

I've discovered I can declare attributes on my page directly:

@using ServiceStack.ServiceInterface
@inherits ViewPage

@{
    new AuthenticateAttribute().Execute(Request, Response, this);
}
<!DOCTYPE html>
...
...
ETC

或者我确定我可以创建一个继承 ViewPage 的类并在其 Init 方法中运行它们并在 Razor 页面上使用新类.

Or I'm sure I could create a class inherit ViewPage and run them in its Init method and use the new class on the Razor pages.

不过,这两种解决方案似乎都是无关紧要的,而且不是很枯燥.

Both of those solutions seem extraneous and not very DRY, though.

推荐答案

我最终创建了自己的 ViewPage 类来实现这一点,并在页面上设置模型后调用过滤器:

I ended up creating my own ViewPage class to achieve this and invoking the filters after the model has been set on the page:

public abstract class FilterViewPage<TModel> : ViewPage<TModel>
    where TModel : class
{
    public override void SetModel(object o)
    {
        base.SetModel(o);

        this.AppHost.RequestFilters.ForEach(action => action.Invoke(this.Request, this.Response, this.Model));
        this.AppHost.ResponseFilters.ForEach(action => action.Invoke(this.Request, this.Response, this.Model));
    }
}

我确保只有顶级页面继承这一点,而不是将其应用于部分页面,因此过滤器只触发一次.

I ensure only top-level pages inherit this, rather than applying it to partials so the filters only fire once.

这篇关于ServiceStack 全局请求过滤器未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆