加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

像ASP.NET WebControls一样,在dll中可重用的ASP.NET MVC组件

发布时间:2020-12-16 07:05:59 所属栏目:asp.Net 来源:网络整理
导读:我有几个网站,这些网站共享几个组件.使用经典的ASP.NET WebControls ascx非常容易.我创建了几个这样的控件,放入一个dll库,我通过namespace.elements runat = server从这些网站引用这些库… 但是在升级到ASP.NET MVC之后我不知道该怎么做.我可以将模型和cotro
我有几个网站,这些网站共享几个组件.使用经典的ASP.NET WebControls ascx非常容易.我创建了几个这样的控件,放入一个dll库,我通过namespace.elements runat = server从这些网站引用这些库…

但是在升级到ASP.NET MVC之后我不知道该怎么做.我可以将模型和cotroller类放入dll中.

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

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

EDITS:
我宁愿选择一些比第三方更标准的解决方案.对我来说,最后一个解决方案是使用StringBuilder而不是ViewEngine.

解决方法

我已经使用 Razor Generator多年来在单独的.dll中存储可重用的MVC视图和帮助程序.

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

Installation instructions

It’s on the VS extension gallery,so install
it from there. It’s called 07001 (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项目,以支持intellisense和其他有用的功能.

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 ‘07002’ 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视觉工作室扩展,另一个但类似于Razor Generator方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读