如何将操作从所有控制器重定向到一个视图? [英] How to redirect action from all controllers to one View ?

查看:79
本文介绍了如何将操作从所有控制器重定向到一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个问题。我的项目中有一些控制器,我尝试创建将此操作重定向到一个视图。我想在特殊情况下创建此选项,例如:我想要,在2014年8月18日21:00的5分钟内阻止应用程序。在这段时间你无所事事,当你尝试点击添加新产品或编辑产品按钮时,你将重定向到新页面,其中包含对不起,申请已关闭的信息。



我的构想是,创建具有条件的全局动作过滤器,例如,如果datetime 18/08/2014 21:00为真,则重新定向到查看信息,否则继续工作。



这是好概念吗?



您对此有何看法?

也许你有其他的解决方案?

Hello,
i have one question. I have some controllers in my project and i try to created redirect this actions to one View. I want to create this option in special cases for example: I want, blocked application on 5 minutes in date 18/08/2014 at 21:00. In this time you can't nothing to do, when you try click on button "Add new product" or "Edit product" you are redirect to new page with information "Sorry, application is off".

My conception is that, create Global action filter with condition where i check for example if datetime 18/08/2014 21:00 is true then redirect to View with information , else continue work.

This is good conception ?

What are you think about that ?
Maybe you have other solution?

推荐答案

首先你需要创建一个Controller并查看错误信息,比如

First you need to create a Controller and view for error message like
public class HomeController : BaseController
{
    public ActionResult Index()
        {
            ViewBag.Message = TempData["Message"];
            return View();
        }
    public ActionResult ErrorMessage()
        {
            TempData["Message"] = "Sorry, application is off";

            return RedirectToAction("Index");
        }
}





然后创建一个动作过滤器,如





Then Create a Action Filter like

public class ApplicationAuthorise : AuthorizeAttribute
    {

        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            if (AppLockedDate==true)
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary {
                        { "Controller", "Home" },
                        { "Action", "ErrorMessage" }
                        });
            }


        }
    }







将授权属性应用于您的操作






Apply authorization attribute to your actions as

[ApplicationAuthorise]
        public ActionResult ShowDetails()
        {
            return View();
        }





我希望它有效。



I hope it works.


这篇关于如何将操作从所有控制器重定向到一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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