简体   繁体   English

MVC 视图是否可以访问所有项目,即使它们未被视图所在的项目引用?

[英]Can MVC Views access all projects even though they aren't referenced by the project where the views are?

Ok, so I'm a bit confused as to what is going on with the following data.好的,所以我对以下数据发生了什么感到有点困惑。

We have the following Structure in our application:我们的应用程序中有以下结构:

  • Portal.Web - An MVC 3 Web App which basically contains all the views, scripts, css and HTML helper extension methods Portal.Web - 一个 MVC 3 Web 应用程序,基本上包含所有视图、脚本、css 和 HTML 辅助扩展方法
  • Portal.Core - A Class Library which is basically our Business Objects, we have all of our models contained within this project. Portal.Core - 一个 Class 库,基本上是我们的业务对象,我们的所有模型都包含在这个项目中。
  • Portal.Data - Another Class Library which contains our NHibernate config and our DTO classes. Portal.Data - 另一个 Class 库,其中包含我们的 NHibernate 配置和我们的 DTO 类。

Here's our usage: In the controller we call the model located in Portal.Core, which populates by calling Portal.Data, so basically Web can never see data.这是我们的用法:在 controller 中,我们调用位于 Portal.Core 中的 model,它通过调用 Portal.Data 进行填充,因此基本上 Web 永远看不到数据。

Here's the catch: In the controller, say for example I try and instantiate a new DTO object called Client like so:关键在于:在 controller 中,例如我尝试实例化一个名为 Client 的新 DTO object,如下所示:

var client = new Client();

It won't work, which is expected it has no idea what Client is and even specifying a using won't cut it.它不会工作,这是预期的,它不知道 Client 是什么,甚至指定一个 using 也不会削减它。 That's fine.没关系。

BUT if I try and do that exact same line in the View, Resharper adds the using to the view and then no complaints, the project runs and we can use DTO classes in our views.但是,如果我尝试在视图中执行完全相同的行,Resharper 会将 using 添加到视图中,然后没有投诉,项目运行,我们可以在我们的视图中使用 DTO 类。

So the question is, why is this?那么问题来了,这是为什么呢? I'm trying to stop our juniors from using DTO classes in Views, so I've purposely removed the reference to the Data project in Web, but they can still use the classes.我试图阻止我们的后辈在视图中使用 DTO 类,所以我特意删除了对 Web 中数据项目的引用,但他们仍然可以使用这些类。 Can someone shed some light?有人可以阐明一下吗?

I ran the same test with ASPX and Razor views.我用 ASPX 和 Razor 视图运行了相同的测试。 Referencing Client in ASPX views fails however in Razor views they work.在 ASPX 视图中引用Client失败,但在 Razor 视图中它们有效。 The views are compiled on the fly when you request the application, so I had a look at some folders in "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\portal.web" and from cmdline files references to the assembly is explicitly added when the view is compiled.当您请求应用程序时,这些视图是即时编译的,所以我查看了“C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\portal.web”中的一些文件夹和 cmdline 文件编译视图时显式添加对程序集的引用。

It seems the process that compiles razor views adds references to all the assemblies in the bin folder.似乎编译 razor 视图的过程添加了对 bin 文件夹中所有程序集的引用。 However, looking at the source of ASP.NET MVC, I cannot confirm this.但是,查看 ASP.NET MVC 的源代码,我无法确认这一点。

So, the only conclusion I can come to is that it is a side effect of using the Razor View Engine.所以,我能得出的唯一结论是,这是使用 Razor 视图引擎的副作用。

That said, you may want to scan the web.config to see if it was included using the assemblies element .也就是说,您可能想要扫描 web.config 以查看它是否包含在使用assemblys 元素中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM