Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared declarative html helpers between projects in Asp.Net MVC

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

like image 578
Samuel Avatar asked Nov 13 '22 09:11

Samuel


1 Answers

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?

like image 189
Bhushan Firake Avatar answered Nov 15 '22 13:11

Bhushan Firake