Common declarative html helpers between projects in Asp.Net MVC

Is it possible to place declarative html helpers in an assembly other than an application web project?

Currently, the only place where I know I can place my declarative html helpers in the "App_Code" folder of my web application project.

After several months of development, I now have many of these declarative html helpers, and the only solution I have to share with it among all my projects is a very bad practice 'Copy the attached version .

Any other ways?

PS

Just don't confuse the definition of what declarative html helpers are, here is an example of code that makes a difference compared to C # extensions that extend the System.Web.Mvc.Html type:

@using Microsoft.Web.Mvc.Html @helper YesNoRadioButton(System.Web.Mvc.HtmlHelper html, string name, bool value, bool disable = false) { @html.RadioButton(name, true, value, id: name.Replace(".", "_") + "_Yes", disabled: disable) @:Oui @html.RadioButton(name, false, !value, id: name.Replace(".", "_") + "_No", disabled: disable) @:Non } @helper YesNoRadioButton(System.Web.Mvc.HtmlHelper html, string name, bool? value, bool disable = false) { @html.RadioButton(name, true, value.HasValue && value.Value, id: name.Replace(".", "_") + "_Yes", disabled: disable) @:Oui @html.RadioButton(name, false, value.HasValue && !value.Value, id: name.Replace(".", "_") + "_No", disabled: disable) @:Non } 

enter image description here

+4
source share
2 answers

Create a class library of these helpers, and then share it between projects. It would be better if you put your helpers in the custom created Helpers folder. This is what I do.

You can refer to this answer: Where should I put my utility classes in an ASP.NET MVC3 application?

+2
source

I wrote an article in which you take the steps necessary to create an HTML helper library. At the end there is a complete example project that you can download from GitHub.

https://www.simple-talk.com/dotnet/asp.net/writing-custom-html-helpers-for-asp.net-mvc/

-1
source

All Articles