MVC:位于另一个项目视图 [英] MVC : Views located in another project

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

问题描述

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

所以,首先,我创建了两个项目的浏览

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

浏览的项目是将只包含意见的类库项目

我的问题是如何使控制器调用位于查看意见项目

我有像这样的控制器:

 公众的ActionResult默认()
        {
            返回this.View();
        }


解决方案

MVC不编译意见纳入DLL的,而是他们作为参考文件,从您的网站的根目录下。位置,按照惯例是〜/视图和一个搜索路径之后。这是多了还是少下功夫coded到默认的视图引擎。

由于视图是文件,当你打破他们到一个单独的项目,他们将不在您的主Web应用程序项目存在。因此,该视图引擎无法找到他们。当您编译应用程序,引用的所有项目将只复制DLL的(和潜在的其他一些东西,比如PDB的,等等。)

现在,有办法解决这个问题,但说实话,他们通常是更多的麻烦比他们的价值。你可以看看在MVC的contrib项目便携式领域,但这些都没有得到很好的支持,并有一个与包装的NuGet取代它们的传言。

您还可以按照@ mo.esmp的建议,并创建一个自定义视图引擎,但你仍然需要找出如何某处网站建立后,可以访问它们和/或部署复制意见。

我的建议是在你所描述的方式不出来的项目。我没有看到它的任何值。如果你的项目变得如此之大,我想不是您的code分离成不同的区域,并保持所有区域code和数据一起。

什么样的​​价值是在有分离这显然取决于对方为单独的组件谁的唯一目的是基于他们的目的,收集物品的事情?我看到在分离模型转化为他们自己的项目一定的价值,因为模型可以由多个组件一起使用。控制器和视图,然而,只有永远使用的MVC主站点。

I want to separate my MVC project into several projects

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

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

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

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

I have controllers like this one:

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

解决方案

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.

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.)

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.

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.

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天全站免登陆