更改 ASP.NET MVC 筛选器中的视图
如果用户使用移动浏览器,我想将用户重定向到不同的视图。我决定使用 MVC 过滤器来完成此操作,将其应用于我想要具有移动视图的操作。
我相信这个重定向需要在 OnActionExecuted 中发生,但是 filterContext 不包含视图上的信息 - 它包含在 OnResultExecuted 中,但此时我认为更改视图为时已晚。
如何拦截视图名称并更改 ViewResult?
这就是我在执行结果中得到的内容以及我希望在执行操作中进行的工作。
public class MobilePageFilter : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
if(filterContext.Result is ViewResult)
{
if (isMobileSite(filterContext.HttpContext.Session[SetMobile.SESSION_USE_MOBILE]))
{
ViewResult viewResult = (ViewResult)filterContext.Result;
string viewName = viewResult.ViewName;
filterContext.Result = new ViewResult
{
ViewName = "Mobile/" + viewName,
ViewData = viewResult.ViewData,
TempData = viewResult.TempData
};
}
}
base.OnResultExecuted(filterContext);
}
}
I want to redirect the user to a different view if they are using a mobile browser. I've decided I'd like to do this using MVC filters by applying it to actions which I want to have a mobile view.
I believe this redirect needs to happen in OnActionExecuted, however the filterContext does not contain information on the view - it does, however in OnResultExecuted, but by this time I believe it is too late to change the view.
How can I intercept the view name and change the ViewResult?
This is what I have in the result executed and what I'd like to have work in Action Executed.
public class MobilePageFilter : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
if(filterContext.Result is ViewResult)
{
if (isMobileSite(filterContext.HttpContext.Session[SetMobile.SESSION_USE_MOBILE]))
{
ViewResult viewResult = (ViewResult)filterContext.Result;
string viewName = viewResult.ViewName;
filterContext.Result = new ViewResult
{
ViewName = "Mobile/" + viewName,
ViewData = viewResult.ViewData,
TempData = viewResult.TempData
};
}
}
base.OnResultExecuted(filterContext);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会向您推荐以下博客文章,它解释了实现您所要求的更好的替代方案for 而不是使用动作过滤器。
I would recommend you the following blog post which explains a better alternative to achieve what you are asking for rather than using action filters.
这就是我最终所做的,并包装成一个可重用的属性,最棒的是它在根据您的要求重定向(或应用您想要的任何结果)时保留原始 URL:
然后只需添加属性
[SiteAccessAuthorise ]
添加到您希望应用授权访问的控制器(在我的例子中)或将其添加到 BaseController。确保您重定向到的底层控制器的操作没有该属性,否则您将陷入无限循环!This is what I ended up doing, and wrapped up into a reusable attribute and the great thing is it retains the original URL while redirecting (or applying whatever result you wish) based on your requirements:
Then just add the attribute
[SiteAccessAuthorise]
to your controllers you wish to apply the authorisation access to (in my case) or add it to a BaseController. Make sure though the action you are redirecting to's underlying controller does not have the attribute though, or you'll be caught in an endless loop!