MVC3"全局变量" [英] MVC3 "Global Variable"

查看:100
本文介绍了MVC3"全局变量"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用Windows身份验证的MVC应用程序内联网。被访问的数据是每年特定的,所以我需要一种方法来设置一个全局变量,无论是会议,或什么的,执行任何疑问之前必须进行设置。

I am creating an MVC Intranet app with windows authentication. The data being accessed is year specific, so I need a way to set a global variable, be it a session, or whatever, that must be set before any queries are performed.

我一直没能找到一种方法来强制用户进行登录,因为他们已授权通过自己的窗口,以便使用属性强制登录似乎有问题登录凭据。

I've not been able to find a way to force a user to login since they are already authorized via their windows login credentials so using attributes to force a login seems problematic.

我发现,用户可以导航到任何特定的页面,然后尝试提取数据,并创建一个有点混乱无定义的一年。

I am finding that users could navigate to any particular page and attempt to pull data and create a bit of havoc without the year defined.

我希望做的是强制用户每年选择页面来选择他们希望访问如果年份变量尚未设置年份。

What I would like to do is force a user to a year select page to select the year which they wish to access if the year variable is not yet set.

我检查了相关的问题,他们似乎并没有提供答案,我目前的难题。

I've checked related questions and they do not appear to provide answer to my current conundrum.

推荐答案

您可以编写自定义ActionFilter,你把所有的控制器(但不上,你必须选择年份,当然动作)。如果一年不被选中,你重定向到控制器/动作,你可以选择一年。

You can write a custom ActionFilter, which you put on all your controllers (but not on the action where you have to select the year, of course). If year ain't be selected, you redirect to the controller/action where you can choose the year.

public class CheckYearAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if(<YEAR IS NOT SET>)//redirect
        {
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary {{ "Controller", "YourController" },
                                      { "Action", "YourAction" } });
        }

        base.OnActionExecuting(filterContext);
    }
}

这篇关于MVC3&QUOT;全局变量&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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