ASP.NET MVC 3 - 可移植区域视图找不到我的模型
我已经开始使用 MvcContrib 的可移植区域,并且对于非常简单的视图来说一切正常,但是当我想在视图中使用自定义模型时,我收到错误消息,指出名称空间不存在。
该视图设置为作为资源嵌入。视图中的智能感知可以很好地识别模型。
有人知道可能导致问题的原因吗?
更新
我认为这可能与我使用 MEF 加载插件有关。加载控制器时我遇到了类似的问题。我必须构建一个自定义 ControllerFactory,如果默认控制器工厂找不到合适的控制器,它将在 MEF 控制器列表中查找。
更新 2
我设法通过向 RazorBuildProvider 提供我的 MEF 加载程序集来消除该错误。然而,现在已经找不到这个视图了。如果视图不是强类型的,那么它就会被找到。
RazorBuildProvider.CodeGenerationStarted += (object sender, EventArgs e) =>
{
RazorBuildProvider provider = (RazorBuildProvider)sender;
foreach (var module in ExternalComponents)
{
provider.AssemblyBuilder.AddAssemblyReference(module.GetType().Assembly);
}
};
源代码
模型 控制器
namespace Isis.Plugins.TextArea.TextArea.Models
{
public class TextAreaModel
{
[Required(ErrorMessage = "Field is required")]
public string Message { get; set; }
}
}
:
namespace Isis.Plugins.TextArea.TextArea.Controllers
{
[Export(typeof(IController))]
public class IndexController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View(new TextAreaModel() { Message = "Hallow!" });
}
[HttpGet]
public ActionResult Editor()
{
return View(new TextAreaModel() { Message = "EDITOR CONTENT" });
}
}
}
视图
@model Isis.Plugins.TextArea.TextArea.Models.TextAreaModel
@Model.Message
错误:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0234: The type or namespace name 'Plugins' does not exist in the namespace 'Isis' (are you missing an assembly reference?)
Source Error:
Line 27:
Line 28:
Line 29: public class _Page_Areas_TextArea_Views_Index_Index_cshtml : System.Web.Mvc.WebViewPage<Isis.Plugins.TextArea.TextArea.Models.TextAreaModel> {
Line 30:
Line 31: #line hidden
I've started using MvcContrib's Portable Areas and everything works fine for the very simple views, but when I want to use a custom model in my view i get the error saying the namespace doesn't exist.
The view is set to be embedded as resource. And intellisense in the view recognizes the model just fine.
Does anybody have any idea what might cause the problem?
UPDATE
I think it might have to do with the fact that i'm using MEF to load the plugins. I had a similar problem when loading the controllers. I had to build a custom ControllerFactory that would look in the MEF Controllers list if no suitable controller was found by the default controllerfactory.
UPDATE 2
I managed to get rid of the error by providing the RazorBuildProvider with my MEF-loaded assemblies. However, now the view is not found anymore. If the view is not strongly typed it IS found.
RazorBuildProvider.CodeGenerationStarted += (object sender, EventArgs e) =>
{
RazorBuildProvider provider = (RazorBuildProvider)sender;
foreach (var module in ExternalComponents)
{
provider.AssemblyBuilder.AddAssemblyReference(module.GetType().Assembly);
}
};
Source Code
The Model
namespace Isis.Plugins.TextArea.TextArea.Models
{
public class TextAreaModel
{
[Required(ErrorMessage = "Field is required")]
public string Message { get; set; }
}
}
The Controller:
namespace Isis.Plugins.TextArea.TextArea.Controllers
{
[Export(typeof(IController))]
public class IndexController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View(new TextAreaModel() { Message = "Hallow!" });
}
[HttpGet]
public ActionResult Editor()
{
return View(new TextAreaModel() { Message = "EDITOR CONTENT" });
}
}
}
The View
@model Isis.Plugins.TextArea.TextArea.Models.TextAreaModel
@Model.Message
The error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0234: The type or namespace name 'Plugins' does not exist in the namespace 'Isis' (are you missing an assembly reference?)
Source Error:
Line 27:
Line 28:
Line 29: public class _Page_Areas_TextArea_Views_Index_Index_cshtml : System.Web.Mvc.WebViewPage<Isis.Plugins.TextArea.TextArea.Models.TextAreaModel> {
Line 30:
Line 31: #line hidden
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 MEF 和 MEF 方面面临类似的问题;剃刀视图引擎(尝试您描述的类似方法)。当我加载强类型剃刀视图时,我收到“您是否缺少程序集/引用”错误。
我尝试将程序集部署在 Bin 下,但这也没有帮助。
避免这种情况的唯一方法是在 RazorBuildProvider 上执行 loadFrom 程序集。
除了“不打算直接从您的代码使用”之外,我找不到任何有关 RazorBuildProvider 的文档。
您的代码片段非常有趣......您能解释一下它是如何工作的吗?预计将在哪里注册 - 在 AppStart 上?
任何清晰度将不胜感激......
I'm facing similar issue with MEF & razor view engine (trying similar approach you have described). When i load my strongly typed razor views, i get "are you missing an assembly/reference" error.
I tried deploying my assemblies under Bin but that didnt help either.
Only way to avoid it is to do a loadFrom assembly on RazorBuildProvider.
I couldn't find any documentation on RazorBuildProvider other than "not intended to be used directly from your code"
You're code snippet is quite interesting... can you please explain how it works? Where is this expected to be registered - at AppStart?
Any clarity would be much appreciated...
我最终决定将所有插件放在 Bin 目录中,而不是自定义的 Plugins 目录中。这不是我想要的解决方案,但它目前有效。
I eventually decided to place all the plugins in the Bin directory instead of a custom Plugins directory. It's not the solution I was aiming for, but it works for now.