是否可以在 ASP.NET MVC 中的多个项目上重用部分视图?
我知道 ASP.NET MVC 3 不支持区域重用,这对于 Web 应用程序的用户管理区域来说非常方便,但是部分视图怎么样?
假设我将 Pager“控件”作为 Razor
(或 WebFormViewEngine
,没关系)部分视图,我可以在 MVC
中轻松重用它> 应用。
除了在新应用程序中创建部分视图并复制粘贴代码之外,是否可以在多个 MVC 应用程序中重用它?
I know ASP.NET MVC 3 doesn't support area reuse, which would be very handy for, say, user admin areas of web applications, but how about partial views?
Say I have Pager "control" as a Razor
(or WebFormViewEngine
, it doesn't matter) partial view which I can easily reuse inside my MVC
application.
Is it possible to reuse it in multiple MVC applications other than by creating a partial view in the new application and copy-pasting the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
框架中没有任何内置功能可以让您执行此操作。您可以查看 MVCContrib 可移植区域,它允许您在多个 ASP.NET MVC 应用程序之间嵌入和重用视图。您还可以找到以下博客帖子很有用。
免责声明:这两种方法都依赖于编写自定义 VirtualPathProvider 它不适用于预编译的 ASP.NET 应用程序。因此,如果您打算在发布之前预编译应用程序,请不要使用它们。就我个人而言,我处于这种情况,我最终做的是编写 自定义 NuGet 包,其中包含所有必要的视图和程序集,其中包含各自的视图模型,开发人员所要做的就是从 Intranet/Internet 的集中位置安装 NuGet 包。
There is nothing buit-in the framework that allows you to do this. You may take a look at MVCContrib portable areas which allows you to embed and reuse views between multiple ASP.NET MVC applications. You may also find the following blog post useful.
Disclaimer: both those approaches rely on writing a custom VirtualPathProvider which doesn't work with precompiled ASP.NET applications. So if you intend to precompile your application before shipping don't use those. Personally I am in this situation and what I ended up doing is writing a custom NuGet package which contains all the necessary views and assemblies containing their respective view models and all that a developer has to do is to install the NuGet package from a centralized location withing the intranet/internet.
Jess Chawick 所著的 O'Reilly 书籍“Programming ASP.NET MVC 4”中有一章描述了您需要什么。
“第 15 章 - 可重用 UI 组件”
基本上,您可以使用视图创建类库项目。您必须安装 RazorGenerator 并在 .cshtml 文件的属性中将其设置为自定义工具。这将从 .cshtml 文件生成 C# 代码。现在,要在 MVC 应用程序中找到标准搜索路径之外的视图,您必须使用 Nuget 包 PrecompiledMvcEngine。
这本书写得很好,您可以找到如何操作的分步信息。
In the O'Reilly book "Programming ASP.NET MVC 4" by Jess Chawick there is a chapter describing what you need.
"CHAPTER 15 - Reusable UI Components"
Basically you create Class Library project with your views. You have to install RazorGenerator and set it as Custom Tool in the properties of the .cshtml files. This will generate C# code from the .cshtml files. Now to locate the views outside your standard search path in your MVC application you have to use the Nuget Package PrecompiledMvcEngine.
The book is very well written and you can find step by step info how to do it.