强类型的视图未找到当加载从外部库 [英] Strongly Typed View Not Found When Loading From External Library

查看:128
本文介绍了强类型的视图未找到当加载从外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我有我试图融入一个可插拔的体系结构的MVC 4应用程序。我加载了被命名为区目录中的DLLVS.Web.IPortal.Apps。*。dll的。

I have an MVC 4 application that I am trying to fit into a pluggable architecture. I am loading dlls within the Areas directory that are named "VS.Web.IPortal.Apps.*.dll".

问题

这是所有工作,使用强类型的意见时除外。是否有任何故障诊断或调试方法,可以帮助理解正在做什么去找了MVC路由引擎寻找的看法?

This is all working, except when using strongly-typed views. Are there any troubleshooting or debugging methods that can help understand what is being done to "find" the view that the MVC Routing engine is looking for?

错误底部

code

这里的code,是有关我在做什么。

Here's the code that is relevant to what I'm doing.

PluggableInterface.dll

一个标准类库,有一个类ApplicationDynamicDiscovery。

A standard class library, with one class ApplicationDynamicDiscovery.

属性/的AssemblyInfo.cs

Properties/AssemblyInfo.cs

[assembly: PreApplicationStartMethod(typeof(ApplicationDynamicDiscovery), "Discover")]

ApplicationDynamicDiscovery.cs

ApplicationDynamicDiscovery.cs

public static class ApplicationDynamicDiscovery
{        
    /// <summary>
    /// Discover all dlls that meet the spec in the path. This is static, so that it happens
    /// before startup - this works.
    /// </summary>
    public static void Discover()
    {
        var areasDir = new DirectoryInfo(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Areas"));

        if (!areasDir.Exists)
        {
            return;
        }

        var assemblies = areasDir.GetDirectories()
                .SelectMany(d => d.GetDirectories("bin", SearchOption.TopDirectoryOnly))
                .SelectMany(d => d.GetFiles("VS.Web.IPortal.Apps.*.dll", SearchOption.TopDirectoryOnly))
                .Select(f => Assembly.LoadFile(f.FullName));

        // register all the assemblies.
        assemblies.ToList().ForEach(BuildManager.AddReferencedAssembly);
    }
}

MainMvcApp
主要的应用程序没有任何自定义code - 这是使用MVC 4互联网应用程序模板创建的。
创建一个项目参考PluggableInterface.dll。

MainMvcApp The main application doesn't have any custom code - it was created using the MVC 4 Internet Application Template. Create a project Reference to PluggableInterface.dll.

MvcApp
创建MainMvcApp的地区文件夹内这一项目。

MvcApp Create this project inside of the Areas folder of MainMvcApp.

MvcAppAreaRegistration.cs

MvcAppAreaRegistration.cs

namespace VS.Web.IPortal.Apps.MvcApp
{
    public class MvcAppAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "MvcApp";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            // Default route
            context.MapRoute(
                    "MvcApp_default",
                    "MvcApp/{controller}/{action}/{id}",
                    new { action = "Index", id = UrlParameter.Optional },
                    new string[] { "VS.Web.IPortal.Apps.MvcApp.Controllers" }
                );
        }
    }
}

控制器/ MvcAppController.cs

Controllers/MvcAppController.cs

namespace VS.Web.IPortal.Apps.MvcApp.Controllers
{
    public class MvcAppController : Controller
    {
        public ActionResult Index()
        {
            return this.View(new MvcAppModel { Name = "Test" });
        }
    }
}

型号/ MvcAppModel.cs

Models/MvcAppModel.cs

namespace VS.Web.IPortal.Apps.MvcApp.Models
{
    public class MvcAppModel
    {
        public string Name { get; set; }
    }
}

查看/ MvcApp / Index.cshtml

Views/MvcApp/Index.cshtml

@model VS.Web.IPortal.Apps.MvcApp.Models.MvcAppModel

<h2>title - @Model.Name </h2>

通过在的地方 - 我可以启动服务器和我浏览到本地主机:端口/ MvcApp / MvcApp我收到以下错误

With that in place - I can start the server and I browse to localhost:port/MvcApp/MvcApp I receive the following error

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/MvcApp/Views/MvcApp/Index.aspx
~/Areas/MvcApp/Views/MvcApp/Index.ascx
~/Areas/MvcApp/Views/Shared/Index.aspx
~/Areas/MvcApp/Views/Shared/Index.ascx
~/Views/MvcApp/Index.aspx
~/Views/MvcApp/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Areas/MvcApp/Views/MvcApp/Index.cshtml
~/Areas/MvcApp/Views/MvcApp/Index.vbhtml
~/Areas/MvcApp/Views/Shared/Index.cshtml
~/Areas/MvcApp/Views/Shared/Index.vbhtml
~/Views/MvcApp/Index.cshtml
~/Views/MvcApp/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml 

如果我从视图中删除 @model VS.Web.IPortal.Apps.MvcApp.Models.MvcAppModel 它的工作原理。
如果我删除参考PluggableInterface项目并添加MvcApp项目它的工作原理与强类型的模型。
任何人都可以点我如何解决此问题的路由将AP preciated。

If I remove the @model VS.Web.IPortal.Apps.MvcApp.Models.MvcAppModel from the view it works. If I remove the reference to the PluggableInterface project and add the MvcApp project it works with the strongly typed model. Anyone that can point me to how to troubleshoot this routing issue would be appreciated.

推荐答案

我通过跟踪组件的我已经添加了他们之后,再加入装配解析器解析这一点。我不知道这是否是最好的解决办法,但似乎这样的伎俩。

I resolved this by keeping track of the assemblies after I've added them, and then adding an assembly resolver. I don't know if it's the best solution, but it seems to do the trick.

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;
ApplicationDynamicDiscovery.Assemblies = new List<Assembly>();

CurrentDomainAssemblyResolve功能:

CurrentDomainAssemblyResolve function:

    /// <summary>
    /// Look through the dynamically loaded assemblies, when looking for an assembly.
    /// </summary>
    private static Assembly CurrentDomainAssemblyResolve(object sender, ResolveEventArgs args)
    {
        if (args.RequestingAssembly != null)
        {
            return args.RequestingAssembly;
        }

        // might return null
        return ApplicationDynamicDiscovery.Assemblies.SingleOrDefault(x => x.FullName == args.Name);
    }

这篇关于强类型的视图未找到当加载从外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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