Layout视图中访问UserProfile

Layout视图中访问UserProfile

本文介绍了如何在_Layout视图中访问UserProfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC应用程序中,我有一个共享的_Layout.cshtml文件,用于显示用户菜单

In my MVC application I have a shared _Layout.cshtml file that displays the menu for a User

在该视图上,我想显示UserProfile实体的信息,该实体是使用SimpleMembership创建的,因此已链接到可以直接在_Layout页面中访问的IPrincipal用户.

On that View I want to display information from the UserProfile entity - created using SimpleMembership and thus linked to the IPrincipal User that can be accessed directly in the _Layout page.

因此,我编写了一个扩展方法,该方法在UnitOfWork上进行调用,如下所示:

So I wrote an extension method that makes a call on my UnitOfWork and looks like this:

    public static UserProfile GetUserProfile(this IPrincipal u)
    {
        IUnitOfWork uow = new UnitOfWork();
        return uow.UserRepository.GetUserProfile(u);
    }

现在这有效,但是它闻起来不好,因为我实例化了UnitOfWork而不是注入它....

Now this works, but it doesn't smell good because I am instantiating the UnitOfWork rather than injecting it....

我有一个看起来像这样的BaseController类:

I have a BaseController class that looks like this:

public class BaseController : Controller
{
    // NOT NECESSARY TO DISPOSE THE UOW IN OUR CONTROLLERS
    // Recall that we let IoC inject the Uow into our controllers
    // We can depend upon on IoC to dispose the UoW for us
    protected MvcApplication.Data.Contracts.IUnitOfWork _Uow { get; set; }
}

(我将一些代码基于此答案: https://stackoverflow.com/a/12820444/150342)

(I based some of my code on this answer: https://stackoverflow.com/a/12820444/150342)

我使用Package Manager安装StructureMap,并且此代码在应用程序启动时运行:

I used Package Manager to install StructureMap and this code runs on application start:

public static class StructuremapMvc
{
    public static void Start()
    {
        IContainer container = IoC.Initialize();
        DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
        GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
    }
}

据我了解,这会将我的具体UnitOfWork类注入到控制器中,并处理UnitOfWork的处理.

As I understand it this injects my concrete UnitOfWork class into my controllers, and handles disposing of the UnitOfWork.

不幸的是,就我对IoC的了解而言,如果我想从Controller以外的其他地方访问UnitOfWork,或者我是否可以将信息从_Layout传递到_Layout,我不确定该怎么办.我的控制器.我想将数据获取到_Layout页面上,但我对如何从那里访问UnitOfWork或如何将UnitOfWork注入扩展方法感到困惑

Unfortunately that is as far as my understanding of the IoC goes, and I'm not sure what to do if I want to access the UnitOfWork from somewhere other than the Controller - or whether I can pass the information to the _Layout from my controller. I want to get data onto the _Layout page and I am confused about how to access the UnitOfWork from there, or how to inject the UnitOfWork into an extension method

推荐答案

将数据放入ViewBag中,然后让_Layout视图将其从其中拉出.

Put the data in the ViewBag and let the _Layout view pull it from there.

您可以将其放入您的 BaseController

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);
    var u = requestContext.HttpContext.User;
    var data = _UoW.UserRepository.GetUserProfile(u);

    ViewBag.UserData = data;
}

然后在布局"视图中呈现数据:

And in your Layout view you render the data:

@ViewBag.UserData

这篇关于如何在_Layout视图中访问UserProfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:20