Bongo
Bongo

Reputation: 3153

ASP.Net MVC HTMLHelper with external HTML

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

Upvotes: 0

Views: 442

Answers (1)

ste-fu
ste-fu

Reputation: 7482

"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.

Upvotes: 1

Related Questions