【问题标题】:How to render a control in MVC 3 Razor using MEF如何使用 MEF 在 MVC 3 Razor 中呈现控件
【发布时间】:2011-08-16 16:46:51
【问题描述】:

首先这是我的错误: 找不到类型或命名空间名称“Web”(您是否缺少 using 指令或程序集引用?)

public class _Page_Plugins_Views_Web_Plugins_Controls_Controls_Menu_cshtml : System.Web.Mvc.WebViewPage<Web.Plugins.Controls.Models.MenuModel> {

我正在使用 MEF 和强类型视图构建一个可插入的 MVC 3.0 Razor 网站。在我开始构建菜单之前,我正在让插件正常工作。菜单是它自己的插件。我用过http://www.thegecko.org/index.php/2010/06/pluggable-mvc-2-0-using-mef-and-strongly-typed-views/comment-page-1/#comment-7130。到目前为止,一切都像我提到的那样,但是当我在布局中渲染菜单时,它会弹出一个错误(见上文)。

下面是执行此操作的代码。为什么它试图渲染一个视图而不仅仅是局部?

我的控制器:

[HandleError]
[ControllerMetaData("Controls")]
[Export(typeof(IController)), PartCreationPolicy(CreationPolicy.NonShared)]
public class ControlsController : MefController
{
    public virtual ActionResult Menu()
    {
        var builder = new MenuModelBuilder(Context.MenuContainer);
        ViewData.Model = builder.BuildModel();

        return PartialView();
    }

}

我的 _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Web.Plugins.Presentation/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/Web.Plugins.Presentation/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/Web.Plugins.Presentation/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>
<body>
<div class="page">
    <header>
        <div id="title">
            <h1>My MVC Application</h1>
        </div>
        <div id="logindisplay">
            @Html.Partial("_LogOnPartial")
        </div>
        <nav>
            <ul id="menu">
                @*<li>@Html.ActionLink("Home", "Index", "Home")</li>
                <li>@Html.ActionLink("About", "About", "Home")</li>*@


                @{
                    Html.Action("Menu", "Controls");
                }
            </ul>
        </nav>
    </header>
    <section id="main">
        @RenderBody()
    </section>
    <footer>
    </footer>
</div>

我的 MefViewEngine.cs

public class MefViewEngine : RazorViewEngine
{
    private readonly string _defaultMaster;

    public MefViewEngine(string pluginPath, IEnumerable<IPluginRegistration> plugins, string defaultMaster)
    {
        _defaultMaster = defaultMaster;
        CreateViewLocations(pluginPath, plugins);
    }

    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        // Set default master page
        if (string.IsNullOrWhiteSpace(masterName) && !controllerContext.IsChildAction)
        {
            masterName = _defaultMaster;
        }

        // Ensure name is correct
        if (masterName.ToLowerInvariant().EndsWith(".cshtml"))
        {
            masterName = masterName.ToLowerInvariant().Replace(".cshtml", string.Empty);
        }

        if (string.IsNullOrWhiteSpace(masterName))
        {
            return base.FindPartialView(controllerContext, viewName, useCache);
            //masterName = _defaultMaster;
        }
        return base.FindView(controllerContext, viewName, masterName, useCache);
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        // If view isn't .aspx, remove master page
        if (!viewPath.ToLowerInvariant().EndsWith(".cshtml"))
        {
            masterPath = string.Empty;
        }

        var nameSpace = controllerContext.Controller.GetType().Namespace;
        //return base.CreateView(controllerContext, viewPath.Replace("%1", nameSpace), masterPath.Replace("%1", nameSpace));

        return base.CreateView(controllerContext, viewPath, masterPath);
    }

    #region Helper Methods

    private void CreateViewLocations(string pluginPath, IEnumerable<IPluginRegistration> plugins)
    {
        IList<string> viewLocations = new List<string>();
        IList<string> masterLocations = new List<string>();
        string pluginLocation;

        foreach (IPluginRegistration plugin in plugins)
        {
            pluginLocation = string.Format("~/{0}/Views/{1}/", pluginPath, plugin.AssemblyName);
            AddViewLocation(viewLocations, pluginLocation);
            AddMasterLocation(masterLocations, pluginLocation);
        }

        pluginLocation = "~/Views/";
        AddViewLocation(viewLocations, pluginLocation);

        ViewLocationFormats = viewLocations.ToArray();
        PartialViewLocationFormats = viewLocations.ToArray();
        MasterLocationFormats = masterLocations.ToArray();

        AreaViewLocationFormats = new[] {
            "~/" + pluginPath + "/Views/{2}/{1}/{0}.cshtml",
            "~/" + pluginPath + "/Views/{2}/{1}/{0}.cshtml",
            "~/" + pluginPath + "/Views/{2}/Shared/{0}.cshtml",
            "~/" + pluginPath + "/Views/{2}/Shared/{0}.cshtml",
        };

        AreaPartialViewLocationFormats = AreaViewLocationFormats;

        AreaMasterLocationFormats = new[] {
            "~/" + pluginPath + "/Views/{2}/{1}/{0}.cshtml",
            "~/" + pluginPath + "/Views/{2}/Shared/{0}.cshtml",
        };
    }

    private static void AddViewLocation(IList<string> viewLocations, string pluginLocation)
    {
        viewLocations.Add(pluginLocation + "{1}/{0}.cshtml");
        viewLocations.Add(pluginLocation + "{1}/{0}.cshtml");
        viewLocations.Add(pluginLocation + "Shared/{0}.cshtml");
        viewLocations.Add(pluginLocation + "Controls/{0}.cshtml");
        viewLocations.Add(pluginLocation + "Shared/{0}.cshtml");
    }

    private static void AddMasterLocation(IList<string> viewLocations, string pluginLocation)
    {
        viewLocations.Add(pluginLocation + "{0}.cshtml");
        viewLocations.Add(pluginLocation + "{1}/{0}.cshtml");
        viewLocations.Add(pluginLocation + "Shared/{0}.cshtml");
    }

    #endregion
}

【问题讨论】:

    标签: c# asp.net-mvc-3 razor mef


    【解决方案1】:

    我不了解 MEF,但根据我对 MVC 的了解,您不应该返回 PartialViewResult 而不是 Action 结果吗?

    public virtual PartialViewResult Menu()
    {
        var builder = new MenuModelBuilder(Context.MenuContainer);
        ViewData.Model = builder.BuildModel();
    
        return PartialView();
    }
    

    【讨论】:

    • ActionResult 是一个抽象类,其中PartialViewResult 是派生类之一。所以他所拥有的一切都很好。
    • 我仍然不知道我做错了什么。原始代码用于 .Net 2,我将其转换为与 .NET 4 和 Razor ViewEngine 一起使用。我想我还有一些工作要做,但为了解决我眼前的问题,我退回到 2.0 并放弃了 Razor 来进行这个项目的 beta 版本。我在更高版本中修改了代码以完全与剃刀视图引擎一起使用,但使用了不同的 MEF 方法,所以我认为插件加载的方式正在解决问题。当我得到一个完整的解决方案时,我会发布代码的链接。
    • 这个链接MEF MVC是我的问题的解决方案。非常好的文章,所以你会很高兴。
    【解决方案2】:

    您是否已将所有命名空间添加到 Views 文件夹中的 web.config 中?如果有的话,您还应该在您的区域中添加对 web.config 文件的相同引用。

    您在 Razor 视图中创建和引用的任何命名空间都必须添加到元素中包含的命名空间集合中。

    【讨论】:

    • 我会调查的。我正在尝试解决方法。我只是很困惑我创建的所有插件是如何工作的,除了我创建的只是为了显示菜单的部分视图。
    • 这不是问题。包含所有命名空间,并将文件夹添加到构建事件中。
    猜你喜欢
    • 2011-09-02
    • 2012-03-03
    • 2012-09-22
    • 1970-01-01
    • 2011-05-19
    • 2013-05-30
    • 2014-09-02
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多