从ActionFilter返回查看 [英] Return View from ActionFilter

查看:120
本文介绍了从ActionFilter返回查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来检查,如果URL中的参数是无效的ActionFilter。
如果它是无效的我有渲染视图。我不想重定向,因为我还需要ActionExecutingContext。
可以在做些什么呢?

 公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
    {
        GUID processIdentifier =(GUID)filterContext.RouteData.Values​​ [processIdentifier];
        //如果没有找到processIdentifier渲染消息的视图和ViewData的一些其他对象
        filterContext.Controller.ViewData.ModelState.AddModelError(WrongProcessIdentifier,你提供的进程ID是无效);
        base.OnActionExecuting(filterContext);
    }


解决方案

HandleErrorAttribute有什么,我一直在寻找。

  filterContext.Result =新的ViewResult
            {
                VIEWNAME =MessagePage
                ViewData的= filterContext.Controller.ViewData,
                TempData的= filterContext.Controller.TempData
            };

I have an ActionFilter that checks if a parameter in the URL is valid. If it is not valid I have to render a View. I dont want to redirect, because I still need the ActionExecutingContext. Can that be done?

    public override void  OnActionExecuting(ActionExecutingContext filterContext)
    {
        Guid processIdentifier = (Guid)filterContext.RouteData.Values["processIdentifier"];
        //if processIdentifier  not found render a view with message and some other objects in ViewData
        filterContext.Controller.ViewData.ModelState.AddModelError("WrongProcessIdentifier", "The process-id you supplied is not valid");
        base.OnActionExecuting(filterContext);
    }

解决方案

HandleErrorAttribute had what I was looking for.

filterContext.Result = new ViewResult
            {
                ViewName = "MessagePage",
                ViewData = filterContext.Controller.ViewData,
                TempData = filterContext.Controller.TempData
            };

这篇关于从ActionFilter返回查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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