Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable ASP.NET MVC components in dll like ASP.NET WebControls

I have several websites and these website share several components. It was quite easy with classic ASP.NET WebControls ascx. I created several such controls, put into one dll library and I reference these libraries from these websites via namespace.elements runat=server...

But I don't know how to do it after I have upgraded to ASP.NET MVC. I can put model and cotroller class into dll.

But how should I put and reuse Views into dll?

I suppose that Views are not compiled into dll, if I can change the View without recompiling the dll.

EDITS: I would prefer some standard solution over third party. The last solution for me is to use StringBuilder instead of ViewEngine.

like image 802
Tomas Kubes Avatar asked Nov 03 '13 08:11

Tomas Kubes


1 Answers

I've been using Razor Generator for several years to store reusable MVC views and helpers in separate .dll.

Razor Generator "is a Custom Tool for Visual Studio that allows processing Razor files at design time instead of runtime, allowing them to be built into an assembly for simpler reuse and distribution."

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

It is quite simple to use:

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 project should be chosen for class library in order to support intellisense and other useful features.

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 written by Jess Chadwick tells that

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.

and suggests using Razor Single File Generator visual studio extension, another one but the similar to Razor Generator approach.

like image 80
Ilya Palkin Avatar answered Nov 15 '22 11:11

Ilya Palkin