是否可以访问位于另一个项目中的 MVC 视图? [英] Is it possible to access MVC Views located in another project?

查看:30
本文介绍了是否可以访问位于另一个项目中的 MVC 视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把我的MVC项目分成几个项目

I want to separate my MVC project into several projects

首先,我创建了两个项目FrontViews

So first of all, I've created two projects Front and Views

Front 项目是一个包含控制器和模型的 Web 应用程序

The Front project is a web application that contains controllers and models

Views 项目是一个仅包含视图的类库项目

The Views project is a class library project that will contains only the views

我的问题是如何让控制器调用位于 Views 项目中的视图

My question is how can I make controllers call views located in the Views project

我有这样的控制器:

public ActionResult Default()
        {
            return this.View();
        }

推荐答案

MVC 不会将视图编译到 DLL 中,而是将它们作为文件从站点目录的根目录引用.按照惯例,位置是 ~/Views 并且遵循搜索路径.这或多或少被硬编码到默认视图引擎中.

MVC does not compile views into DLL's, but instead references them as files from the root of your site directory. The location, by convention is ~/Views and a search path is followed. This is more or less hard coded into the default view engines.

因为视图是文件,所以当您将它们分解为单独的项目时,它们将不存在于您的主要 Web 应用程序项目中.因此,视图引擎无法找到它们.编译应用程序时,任何引用的项目都只会复制 DLL(可能还有其他一些东西,例如 pdb 等)

Because Views are files, when you break them into a separate project, they won't exist in your primary web application project. Thus, the view engine can't find them. When you compile the app, any projects referenced will only copy the DLL's (and potentially a few other things, like pdb's, etc.)

现在,有一些方法可以解决这个问题,但老实说,它们通常会带来更多的麻烦而不是它们的价值.您可以在 mvc contrib 项目中查看可移植区域",但这些都没有得到很好的支持,并且有人讨论用 NuGet 打包替换它们.

Now, there are ways to work around this, but to be honest, they're usually more trouble than they're worth. You can look into "Portable Areas" in the mvc contrib project, but these are not well supported and there's been talk of replacing them with NuGet packaging.

您也可以按照@mo.esmp 的建议创建自定义视图引擎,但您仍然需要想办法将视图复制到站点可以在构建和/或部署时访问它们的地方.

You can also follow @mo.esmp's advice, and create a custom view engine, but you'll still need to figure out ways to copy the Views somewhere the site can access them upon build and/or deploy.

我的建议是不要以您描述的方式分解项目.我看不出它有任何价值.如果您的项目变得如此庞大,我会将您的代码分成多个区域,并将所有区域代码和数据放在一起.

My suggestion would be to NOT break out projects in the manner you describe. I don't see any value in it. If your project becomes so large, I would instead separate your code into areas, and keep all your area code and data together.

将明显相互依赖的项目分成单独的程序集,这些程序集的唯一目的是根据其目的收集东西,这有什么价值?我看到将模型分离到他们自己的项目中的一些价值,因为模型可以被多个程序集使用.但是,控制器和视图仅由 MVC 主站点使用.

What value is there in separating items that are clearly dependent upon each other into separate assemblies who's only purpose is to collect things based on their purpose? I see some value in separating models into their own project, since models can be used by more than one assembly. Controllers and views, however, are only ever used by the MVC primary site.

这篇关于是否可以访问位于另一个项目中的 MVC 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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