Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put shared HTML generation code in an ASP.NET MVC project?

I have an ASP.NET MVC solution, and I have a large number of HtmlHelper classes inside the ASP.NET MVC project that take data and generate various snippets of html to display on various pages.

I now have to run a bunch of scheduled jobs and many of those jobs generate emails. I moved all of these jobs into a separate project in the solution (called AdminJob.csproj), but I now find that I have to generate a number of email with very similar HTML as I have in my view pages. I am trying to keep the admin jobs from having a dependency on the ASP.NET MVC project (as I would like to run the adminjob project as a command line if required), and I also want to avoid having my ASP.NET MVC project having a dependency on the AdminJob project (as that just seems odd).

Any suggestions on how I can avoid duplicating the same HTML rendering code in both my ASP.NET MVC project and my AdminJob project?

The only thing I can think about is creating another project in the solution called "ViewHelpers" or something like that and move all of my HTMLHelpers into that project so it can be referenced by both my MVC project and the AdminJob project. It seems a bit overkill to have a separate project just for this code, but I can't think of another solution that doesn't create duplication.

Any other suggestions for a better way to do this and avoid any duplication?

like image 837
leora Avatar asked Jan 17 '17 00:01

leora


People also ask

How can add HTML file in MVC project?

Step 1: Right click on the "Controllers" folder and add "LoadHtml" controller. Copy and paste the following code. Step 2: Right click on the "Index" action method in the "LoadHtmlController" and add "Index" view.

Which ASP.NET MVC object generates the HTML output?

The ActionLink() and DisplayNameFor() are extension methods included in the HtmlHelper class. The HtmlHelper class generates HTML elements. For example, @Html.

How can add Cshtml file in ASP.NET MVC?

Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with Layout (Razor). In the Specify Name for Item dialog box, enter Index, and then click OK. In the Select a Layout Page dialog, accept the default _Layout. cshtml and click OK.

What is shared view in MVC?

If a view isn't found in this location, then by convention the MVC runtime looks in the “Views\Shared”. This simple organization scheme works well for small projects, but can become quite unwieldy as the size of the web site grows and the shared folder becomes an ever larger dumping ground.


1 Answers

The way I look at this is that the email is a view type.There are several view types in my web apps (html, email, pdf, rss etc). There are several implementations of this pattern: ActionMailer in ruby on rails or its port to asp.net: MvcMailer.

This way you can take full advantage of the Razor engine in both your web application and your emails in order to reduce duplication.

You can host your views in another project and inform your ViewEngine about where to look for the views (see Views in separate assemblies in ASP.NET MVC). I find this to be overkill.

Another way - the one I use - is to have the email templates in the same project with other views.

If you want to render the views outside of a web application you can compile the razor templates in a console app using one of these:

  • RazorTemplates: https://github.com/volkovku/RazorTemplates
  • RazorEngine: https://github.com/Antaris/RazorEngine
  • with something like MvcMailer you can just make curl requests in your scheduled tasks. MvcMailer needs an HttpContext to run so you wouldn't be able to run it from a console app. curl the mvc actions would work.
like image 65
dexter Avatar answered Nov 08 '22 15:11

dexter