RazorGenerator, Templates, and @Html

天大地大妈咪最大 提交于 2019-11-28 03:51:50

问题


I'm trying to use RazorGenerator as an email template engine. I want to take a model with the data, assemble the correct set of partial views, and return HTML that I can email out.
Edit: In addition to this workflow, any solution would need to be editable as a .cshtml file and be able to be compiled into a dll. For various reasons, it is not practical to deploy the cshtml files themselves - if we can't embed all our razor views into a single file, then we can't use that suggestion. Hence RazorGenerator.

So far, I've worked out every part of it, except for the partials. When I try to use @Html.Partial() in a template file, I get: The name 'Html' does not exist in the current context.

Based on this answer, I know that @Html isn't part of Razor by default, and there's many answers out there as to how to create a HtmlHelper in a controller. However, I need to create one in a template, which doesn't have the ControllerContext that I'd need to follow those examples.

I've also tried using @Include, but the RazorGenerator template doesn't seem to support that. Edit: I also tried creating a new class which inherited from TemplateBase<> and copied all the functionality of RazorTemplateBase, but I get NullReferenceExceptions on the @Include line.

So, my primary question is: Is there a better way to include another Razor file (with model) into my file?

Secondarily, if there isn't a better way, how can I get the HtmlHelper created?


Edit for bounty: Just to reiterate, the four things I need in an acceptable answer are:

  1. The ability to edit .cshtml files with the standard editor (no "store it as a string" or such)
  2. The ability to compile everything into a single dll which can be deployed using our current build system (we can't deploy lots of individual .cshtml's)
  3. The ability to reference one .cshtml file from another, and pass a model - equivalent to @Includes or @Html.Partial (Either of which are perfectly acceptable if they work)
  4. The ability to email out the result, with attachments. (I already have code for this, if the result is a string or convertible to one.)

I currently can get most combinations of three of these working, but I can't get all four at once. I'm open to new libraries, replacing RazorGenerator, or throwing out any part of what I already have, so long as the result works as needed.


回答1:


Just a thought, but why couldn't you set up other pages and in your Controller Code open up an HTTPWebRequest / WebClient send the data you need there, get all the html/text out of that view, merge several calls together and then email out all that string.

public ActionResult SomeAction() {
  // call other section logic using HttpWebRequest or WebClient
  // /controller/emailsection/{vars}/......
  // Get the string out of the request add it to ViewData["xxx"]
  // rinse and repeat for other sections

}

public ActionResult EmailSection() {
  //put section logic here

  Response.ContentType = "text/html"; // "text/plain"
  Response.Write("Some HttpWebResponse");
  return null;
}



回答2:


Is this what you want?

Render a view as a string

How to Email Screen in ASP.Net/MVC

http://www.brightmix.com/blog/how-to-renderpartial-to-string-in-asp-net-mvc/



来源:https://stackoverflow.com/questions/13035427/razorgenerator-templates-and-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!