gpt4 book ai didi

asp.net - dll 中可重用的 ASP.NET MVC 组件,如 ASP.NET WebControls

转载 作者:行者123 更新时间:2023-12-04 18:43:21 25 4
gpt4 key购买 nike

我有几个网站,这些网站共享几个组件。使用经典的 ASP.NET WebControls ascx 非常容易。我创建了几个这样的控件,放入一个 dll 库中,然后我通过 namespace.elements runat=server 从这些网站引用这些库...

但是升级到ASP.NET MVC后不知道怎么做。我可以将模型和 Controller 类放入 dll。

但是我应该如何将 View 放入和重用到 dll 中?

如果我可以在不重新编译 dll 的情况下更改 View ,我想 View 不会编译成 dll。

编辑:
我更喜欢一些标准的解决方案而不是第三方。我的最后一个解决方案是使用 StringBuilder 而不是 ViewEngine。

最佳答案

我一直在用 Razor Generator几年来将可重用的 MVC View 和帮助程序存储在单独的 .dll 中。

Razor 发电机 “是 Visual Studio 的自定义工具,允许在设计时而不是运行时处理 Razor 文件,允许将它们构建到程序集中,以便更简单地重用和分发。”

Installation instructions

It’s on the VS extension gallery, so install it from there. It’s called “Razor Generator” (not to be confused with “Razor Single File Generator for MVC”).



使用起来非常简单:

Usage in an MVC app

  1. Install the 'RazorGenerator.Mvc' package, which registers a special view engine
  2. Go to an MVC Razor view's property and set the Custom tool to RazorGenerator
  3. Optionally specify a value for Custom Tool Namespace to specify a namespace for the generated file. The project namespace is used by default.
  4. Optionally specify one of the generators in the first line of your Razor file. A generator declaration line looks like this: @* Generator: MvcHelper *@ . If you don't specify this, a generator is picked based on convention (e.g. files under Views are treated as MvcViews)
  5. You'll see a generated .cs file under the .cshtml file, which will be used at runtime instead of the .cshtml file
  6. You can also go to the nuget Package Manager Console and run 'Enable-RazorGenerator' to enable the Custom Tool on all the views.
  7. And to cause all the views to be regenerated, go to the nuget Package Manager Console and run 'Redo-RazorGenerator'. This is useful when you update the generator package and it needs to generate different code.


应为类库选择 MVC 项目,以支持智能感知和其他有用的功能。

Usage in a View Library

If you need to create a separate library for your precompiled MVC views, the best approach is to actually create an MVC project for that library, instead of a library project. You'll never actually run it as an Mvc app, but the fact that it comes with the right set of config files allows intellisense and other things to work a lot better than in a library project.

You can then add a reference to that 'MVC View project' from your real MVC app.

And note that you need to install the 'RazorGenerator.Mvc' package into the library, not the main MVC app.



Programming ASP.NET MVC 4杰西·查德威克 (Jess Chadwick) 所著

In the ASP.NET Web Forms world, you can achieve this by creating user controls or custom controls that can be compiled into standalone assemblies. These assemblies can be distributed across projects, thereby enabling their reuse across projects.

The Web Forms view engine offers the ViewUserControl class, which can be leveraged to create such components for the MVC framework. The Razor view engine in ASP.NET MVC, however, does not offer any such method out of the box.



并建议使用 Razor Single File Generator Visual Studio 扩展,另一个但类似于 Razor Generator方法。

关于asp.net - dll 中可重用的 ASP.NET MVC 组件,如 ASP.NET WebControls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19751101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com