5

Is it possible to put the declarative html helpers in an assembly different than the application web project?

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

After a couple of months of development, I now have a lot of these declarative html helpers and the only one solution I have to shared it among all my projects is with a the very bad practice of 'Copy Paste Driven Development'.

Any other ways?

P.S.

Just not to be confused by the definition of what is declarative html helpers, here is a code sample to makes the 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

Samuel
  • 12,073
  • 5
  • 49
  • 71

3 Answers3

2

Make a class library of those helpers and then share it among projects.It will be better if you put your helpers in a custom created folder Helpers. This is what I do.

You can refer to this answer: Where shall I put my utilities classes in a ASP.NET MVC3 application?

Community
  • 1
  • 1
Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
  • 1
    Sorry Bhushan if I'm a little by confusing but I know how to create libraries and the usage of extensions. :) But html helpers are not simple extension (e.g. static classes used to generate html code in case of web application); htlm helpers are partial html views where with a specific syntax you can define html like user controls. By default, Asp.Net MVC searched into the App_Code folder to find this type of 'partial views'. I will change my question to add a more specific example of what is html helpers. – Samuel Mar 11 '13 at 19:48
0

Yes it is possible, all I had to do was put @using at the top of my razor pages to reference the other assembly that had the Helper functions.

Or, if you want to save yourself adding this using at the top of lots of files, you can add the reference to your Web.config file that exists under your Views folder. You might have to close and re-open your razor pages to help Visual Studio acknowledge the new assembly reference.

Nacht
  • 3,342
  • 4
  • 26
  • 41
-1

I wrote an article that takes you through the steps required to make a HTML helper library. There is a full project example at the end which you can download from GitHub.

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

Ed Charbeneau
  • 4,501
  • 23
  • 23
  • I mislead of term; the exact term is 'Declarative html helper' and it's not what you talk about in your post. In anyways, by the nature of how this type of helper is defined, I think it's impossible to put the code outside of the web project application. I must transform my 'declarative html helpers' in 'html helper' with the help of c# extensions, tag builder, etc... Thank you. – Samuel Mar 12 '13 at 14:12
  • Ah, I understand. Yes, I would convert it over. It would take minimal effort to make it portable that way. You can extend the Html.RadioButton in the exact same way as you are in the declarative helper. – Ed Charbeneau Mar 12 '13 at 15:29
  • link-only answers are not answers. Please include all relevant information in your answer so that the answer can stand on its own in case the link goes away in the future. – zzzzBov Jan 11 '17 at 18:56
  • Seriously, this answer is over 3 years old. Find a better outlet for your time. – Ed Charbeneau Jan 12 '17 at 21:51