如何从ASP.NET MVC中的基本控制器连接通用代码 [英] How to wire common code from a base controller in ASP.NET MVC

查看:71
本文介绍了如何从ASP.NET MVC中的基本控制器连接通用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ASP.NET MVC应用程序只是较大的ColdFusion应用程序的一小部分,很快将被完全替换.我正在通过cookie从ColdFusion部分传递一些参数,并且需要在运行每个操作之前检查此信息.如果信息丢失,我需要重定向到父站点.放置此功能的最佳位置是什么?如何统一调用它?

My ASP.NET MVC application is a small part of a bigger ColdFusion app that is going to be soon replaced completely. I'm passing some parameters from ColdFusion part through cookies and need to check for this information before I run every action. In case if the information is missing I need to redirect to the parent site. What is the best place to put this functionality and how to call it uniformally?

当前,我已经实现了基本控制器,并且在每个操作方法中,我都从基本控制器调用方法,并根据返回结果重定向或继续执行操作.这种方法似乎有效,但由于与操作没有直接关系,因此会使我的操作方法混乱.我该如何将其分离出来,是否有控制器可以使用的生命周期事件?

Currently, I have implemented a base controller and in every action method I call a method from the base controller and based on the return result either redirect or continue with action. This approach seems to work but it clutters my action methods with consern not directly related to the action. How could I separate it out, are there any lifecycle events for controller I could tap into?

推荐答案

如果您已经实现了基本控制器,则只需覆盖其OnActionExecuting()方法:

If you already implemented a base controller just override its OnActionExecuting() method:

public class YourBaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if(somethingIsWrong)
        {
            filterContext.Result = new RedirectToRouteResult(
                new System.Web.Routing.RouteValueDictionary { ... });
        }
    }
}

这篇关于如何从ASP.NET MVC中的基本控制器连接通用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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