剃刀MVC,在那里把全局变量是整个母版页,partiview和视图访问? [英] Razor MVC, where to put global variables that's accessible across master page, partiview and view?

查看:165
本文介绍了剃刀MVC,在那里把全局变量是整个母版页,partiview和视图访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好剃刀MVC大师:

新手问题。

背景。我有一个在一个HttpModule之前它获取控制器和功放设置自定义的IIdentity;意见。要使用它,我必须做的。

Background. I have a custom IIdentity that is set in an HttpModule before it gets to controller & views. To use it, i have to do

   MyIdentity myIdentity = (MyIdentity)((GenericPrincipal)context.User).Identity;
   MyComplexUser user = myIdentity.User;
   //user.name //user.location //user.username  //etc

问题是,我用在不同的地方的对象,例如

The problem is, I use the object in different places such as


  • 总体布局

  • 某些子级嵌套布局

  • 某些partialviews

  • 的几点看法

这真的要看是什么性质的MyComplexUser反对的意见,需要。

It really depends on what properties of "MyComplexUser" object the views need.

目前,在视图中,我不得不这样做非常复杂的铸造,获得一个属性。举例来说,如果我想在用户的名,我需要做的。

Currently, in the views, I have to do this really complicated casting to get to a property. For instance, if I want the "Name" of the user, I need to do

@(((MyComplexUser)(((MyIdentity)((的GenericPrincipal)context.User).Identity)。用户))。名称)

@(((MyComplexUser)(((MyIdentity)((GenericPrincipal)context.User).Identity).User)).Name)

我想我可以把它的控制器,然后填充ViewBag用ViewBag.MyUser属性,但是这样

I suppose I could put it in the controllers and then populate the ViewBag with a ViewBag.MyUser property, but then


  1. 我不喜欢用ViewBag。我preFER强类型对象

  2. 如果我用一个强类型的对象(MYUSER)的意见,那我也风靡了MYUSER属性的车型。感觉有点脏?正如我喜欢让我的模型清洁,具体到他们参与的意见。此外,它会是不必要的重复。

  3. 在像master_layout.cshtml或partialviews,地方你怎么进入MYUSER如果我把它们放在一个控制器?

  4. 使用和的RenderAction建立每个用户属性partialviews是矫枉过正?

感谢。同样,我在MVC 4的新手,任何建议是极大的AP preciate它。

Thanks. Again, I'm a newbie at MVC 4, any suggestion is greatly appreciate it.

推荐答案

我会解释,工程pretty很适合我类似的解决方案。有了小的变化,我的认为的,它会为你工作(和其他人,希望)为好。

I'll explain a similar solution that works pretty well for me. With small changes, I believe that it will work for you (and others, hopefully) as well.

基本上,我们将使用继承

让我们创建一个自定义的基本控制,如

Let's create a custom base controller, such as

public class BaseController : Controller

让我们改变我们的控制器来继承它,因为

and let's change our controllers to inherit from it, as

public class HomeController : BaseController

模型(的ViewModels,我说的)

您可能会有很多类的模型文件夹内,对不对?他们作为从控制器到DTO的意见,right²?
如果你的回答的的两个,那么请继续阅读。

Models (ViewModels, I say)

You probably have lots of classes inside your Models folder, right? They act as DTOs from the controller to the views, right²? If you answered yes for both, then keep reading.

让我们创建一个基础模型类,如公共类BaseVM ,让我们改变我们的模型来继承它,就像公共类HomeIndex: BaseVM

Let's create a base model class, such as public class BaseVM, and let's change our models to inherit from it, like public class HomeIndex : BaseVM

重要:您的布局文件( _layout 或任何)必须强类型为 BaseVM 或它的孩子。

Important: your layout file (_Layout or whatsoever) must be strongly typed to BaseVM or a child of it.

现在,一切的漂亮类型,让我们使用对我们有利的请求管道。
BaseController ,您会添加类似如下的方法:

Now that everything's beautifuly typed, let's use the request pipeline in our favor. At BaseController, you'll add a method that looks like this:

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    if (filterContext.Result is ViewResultBase)//Gets ViewResult and PartialViewResult
    {
        object viewModel = ((ViewResultBase)filterContext.Result).Model;

        if (viewModel != null && viewModel is BaseVM)
        {
            BaseVM baseVM = viewModel as BaseVM;

            baseVM.MyIdentity = (MyIdentity)((GenericPrincipal)context.User).Identity;
            //and so on...
        }
    }

    base.OnActionExecuted(filterContext);//this is important!
}

OnActionExecuted 被称为执行的动作,但之前视图渲染。这正是我们想要的。

OnActionExecuted is called after the execution of the action but before the view rendering. That's exactly what we want.

我希望你得到它了。 =)

I hope you got it already. =)

这篇关于剃刀MVC,在那里把全局变量是整个母版页,partiview和视图访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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