0

I saw some Tutorials on ASP.Net MVC HtmlHelpers and they always included the HTML directly into the SourceCode.

I want to create reusable Controls so that I don't have to write a Login view and the parts of it over and over again over the next projects.

The best thing would be if I could write a DLL and place all my created user controls therein

Some time ago I wrote an application with AngularJS and there were directives and in them was a templateUrl. Is there something similar in Asp.Net MVC ?

I am using the Razor View Engine and the .Net Framework 4.0.

I know I could use partial views but partial views seem to not work in dlls

Bongo
  • 2,933
  • 5
  • 36
  • 67
  • 2
    `The best thing would be if I could write a DLL and place all my created user controls therein` have you tried this? – JamieD77 Jan 27 '16 at 16:27
  • Here's similar [answer](http://stackoverflow.com/a/33619432/881798) about re-usability and resource sharing across MVC projects(Dlls) in a solution. – vendettamit Jan 27 '16 at 16:37
  • @JamieD77 I Created a MVC project and included it in another Project but my views werent accesible – Bongo Jan 27 '16 at 16:41
  • @JamieD77 in regard of the HTMLHelper I guess this should work, but still leaves the problem that I don't want the HTML in the C# part – Bongo Jan 27 '16 at 16:48
  • 1
    @Bongo there is an extension called Razor Generator that allows you to precompile mvc projects into a dll and reference that dll in other mvc projects.. it takes a little work to get it working but we use it where i work and have 1 main UI project and 18 compiled mvc projects.. https://visualstudiogallery.msdn.microsoft.com/1f6ec6ff-e89b-4c47-8e79-d2d68df894ec – JamieD77 Jan 27 '16 at 17:00
  • @JamieD77 thanks I will give that a try – Bongo Jan 27 '16 at 17:18

1 Answers1

1

"The best thing would be if I could write a DLL and place all my created user controls therein" - You can. There is one little cheat which makes it all work really easily.

When you are writing your html helpers, make sure that you change the namespace to System.Web.Mvc.Html.

If you use the TagBuilder class then you shouldn't be using too much html in your C# code.

Then if you reference your dll in the project, you should be able to access the html helper from your razor view

You can use other namespaces, but you have to have to edit the web.config file inside the Views folder and add a reference to the namespace in the <system.web.webPages.razor> section. By re-using the already referenced namespace, you can save yourself some configuration hassles.

Depending on how many projects and how many developers you want to share the code between, you could also consider a build server product (My team used TeamCity for about 2 years before we needed to pay for a licence). You can then produce your own custom NuGet packages, which lets you share (and manage updates) for partial views, editor templates, html helpers and much more.

ste-fu
  • 6,879
  • 3
  • 27
  • 46
  • That sounds very interesting but I am not willing to pay for a licence. I will look into your other solution tomorow if you don't mind and see if its working for me – Bongo Jan 27 '16 at 17:21
  • Last time I checked it was free for commercial use for 20 projects, but it's overkill for an individual dev or a small team. I don't work for them... – ste-fu Jan 27 '16 at 19:04