Asp.net mvc 从外部库加载时未找到强类型视图
背景 我有一个MVC4应用程序,我正试图将其融入一个可插拔的体系结构中。我正在加载名为“VS.Web.IPortal.Apps.*.dll”的区域目录中的dll 问题 除了使用强类型视图外,这一切都正常工作。是否有任何故障排除或调试方法可以帮助理解如何“找到”MVC路由引擎正在寻找的视图 底部错误 代码 这是与我正在做的事情相关的代码 PluggableInterface.dll 一个标准类库,具有一个类ApplicationDynamicDiscovery 属性/AssemblyInfo.csAsp.net mvc 从外部库加载时未找到强类型视图,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,背景 我有一个MVC4应用程序,我正试图将其融入一个可插拔的体系结构中。我正在加载名为“VS.Web.IPortal.Apps.*.dll”的区域目录中的dll 问题 除了使用强类型视图外,这一切都正常工作。是否有任何故障排除或调试方法可以帮助理解如何“找到”MVC路由引擎正在寻找的视图 底部错误 代码 这是与我正在做的事情相关的代码 PluggableInterface.dll 一个标准类库,具有一个类ApplicationDynamicDiscovery 属性/AssemblyInfo.cs
[assembly: PreApplicationStartMethod(typeof(ApplicationDynamicDiscovery), "Discover")]
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);
}
}
控制器/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
namespace VS.Web.IPortal.Apps.MvcApp.Models
{
public class MvcAppModel
{
public string Name { get; set; }
}
}
视图/MvcApp/Index.cshtml
@model VS.Web.IPortal.Apps.MvcApp.Models.MvcAppModel
<h2>title - @Model.Name </h2>
如果我从视图中删除@model VS.Web.IPortal.Apps.MvcApp.Models.MvcAppModel
,它将正常工作。
如果我删除对PluggableInterface项目的引用并添加MvcApp项目,它将与强类型模型一起工作。
任何能告诉我如何解决这个路由问题的人都将不胜感激。我通过在添加程序集后跟踪它们,然后添加一个程序集解析程序来解决这个问题。我不知道这是否是最好的解决方案,但它似乎奏效了
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;
ApplicationDynamicDiscovery.Assemblies = new List<Assembly>();
AppDomain.CurrentDomain.AssemblyResolve+=CurrentDomainAssemblyResolve;
ApplicationDynamicDiscovery.Assemblys=新列表();
CurrentDomainAssemblyResolve函数:
/// <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);
}
//
///查找程序集时,请查看动态加载的程序集。
///
私有静态程序集CurrentDomainAssemblyResolve(对象发送方,ResolveEventArgs args args)
{
如果(args.RequestingAssembly!=null)
{
返回args.RequestingAssembly;
}
//可能返回null
返回ApplicationDynamicDiscovery.Assemblys.SingleOrDefault(x=>x.FullName==args.Name);
}
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainAssemblyResolve;
ApplicationDynamicDiscovery.Assemblies = new List<Assembly>();
/// <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);
}