Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

c# - RazorGenerator, Templates, and @Html

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...