从 AuthorizeAttribute 扩展添加到 ViewData[] 集合
我编写了一个扩展类来为我的操作方法自定义 AuthorizeAttribute,并且我希望能够在满足特定条件时将消息注入到我的视图中。当用户未经授权时,我使用下面的代码加载共享视图,但它不会将我的消息添加到我的 ViewData 集合中。有什么想法吗?
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (IsNotAuthorized)
{
filterContext.Result = new ViewResult { ViewName = "NotAuthorized" };
filterContext.Controller.ViewData["Message"] = "Go Away";
}
}
我还尝试将我的 ViewData["Message"] 集合项设置在调用更改视图没有成功。
I wrote an extension class to customize my AuthorizeAttribute for my action methods and I'd like to be able to inject messages into my view when a certain condition is met. I"m using the below code to load up a shared view when a user is not authorized but it's not adding my message to my ViewData collection. Any ideas?
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (IsNotAuthorized)
{
filterContext.Result = new ViewResult { ViewName = "NotAuthorized" };
filterContext.Controller.ViewData["Message"] = "Go Away";
}
}
I've also tried setting my ViewData["Message"] collection item above the call to change the view with no success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试过吗?
我不知道如何添加 ViewData,但这至少会让您到达未经授权的控制器。
我将继续寻找代码来添加以同时查看数据或直到有人发布它。
编辑
这可能会有所帮助;
更改自定义过滤器属性中的 ActionExecutingContext 值
Have you tried;
I don't know how to add the ViewData but this will get you to the not authorised controller at least.
I'll keep looking for code to add to view data in the mean time or until someone posts it.
edit
This may help;
Changing ActionExecutingContext values in Custom Filter Attribute