Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1868080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:38:26+00:00 2026-05-17T06:38:26+00:00

I thought it would be interesting if I could use the new MVC Razor

  • 0

I thought it would be interesting if I could use the new MVC Razor View engine as a mail merge technology. It can still be part of an MVC website and does not have to be stand-alone console app.

Example:

string  myTemplate = "Hello @Name,  How are you today?";
ViewModel.Name = "Billy Boy";
string output = RazorViewEngineRender( myTemplate, ViewModel );

Then the string output = "Hello Billy Boy, How are you today?"

The main thing is I want the template to be driven from a string rather than a view or partialview.

Does anyone know if this is possible ?

UPDATE:

Ben and Matt made a project on codeplex: http://razorengine.codeplex.com/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:38:27+00:00Added an answer on May 17, 2026 at 6:38 am

    Warning

    This is some ugly ugly code that was hacked together without testing it other than getting it to work properly.

    VirtualPathProvider

    Since we’re not dealing with actual views on the server we have to add our own path provider to tell MVC where to get our dynamically generated templates. There should be more tests like checking the strings Dictionary to see if the view has been added.

    public class StringPathProvider : VirtualPathProvider {
        public StringPathProvider()
            : base() {
        }
    
        public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) {
            return null;
        }
    
        public override bool FileExists(string virtualPath) {
            if (virtualPath.StartsWith("/stringviews") || virtualPath.StartsWith("~/stringviews"))
                return true;
    
            return base.FileExists(virtualPath);
        }
    
        public override VirtualFile GetFile(string virtualPath) {
            if (virtualPath.StartsWith("/stringviews") || virtualPath.StartsWith("~/stringviews"))
                return new StringVirtualFile(virtualPath);
    
            return base.GetFile(virtualPath);
        }
    
        public class StringVirtualFile : System.Web.Hosting.VirtualFile {
    
            string path;
    
            public StringVirtualFile(string path)
                : base(path) {
                //deal with this later
                    this.path = path;
            }
    
            public override System.IO.Stream Open() {
                return new System.IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(RazorViewEngineRender.strings[System.IO.Path.GetFileName(path)]));
            }
        }
    }
    

    Render Class

    This class takes your template as a constructor parameter and adds it to a static Dictionary that is then read by the VirtualPathProvider above. You then call Render and you can optionally pass in a model. This will add the fully qualified model type to the @inherits and prepend that to the file contents.

    public class RazorViewEngineRender {
        internal static Dictionary<string, string> strings { get; set; }
    
        string guid;
    
        static RazorViewEngineRender() {
            strings = new Dictionary<string, string>();
        }
    
        public RazorViewEngineRender(string Template) {
            guid = Guid.NewGuid().ToString() + ".cshtml";
            strings.Add(guid, Template);
        }
    
        public string Render() {
            return Render(null);
        }
    
        public string Render(object ViewModel) {
            //Register model type
            if (ViewModel == null) {
                strings[guid] = "@inherits System.Web.Mvc.WebViewPage\r\n" + strings[guid];
            } else {
                strings[guid] = "@inherits System.Web.Mvc.WebViewPage<" + ViewModel.GetType().FullName + ">\r\n" + strings[guid];
            }
    
            CshtmlView view = new CshtmlView("/stringviews/" + guid);
    
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.TextWriter tw = new System.IO.StringWriter(sb);
    
            ControllerContext controller = new ControllerContext();
    
            ViewDataDictionary ViewData = new ViewDataDictionary();
            ViewData.Model = ViewModel;
    
            view.Render(new ViewContext(controller, view, ViewData, new TempDataDictionary(), tw), tw);
            //view.ExecutePageHierarchy();
    
            strings.Remove(guid);
    
            return sb.ToString();
    
        }
    }
    

    Global.asax

    In your global.asax file you’ll have to add the following to the Application_Start

    System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new Controllers.StringPathProvider());
    

    Calling the code

    string Template = "Hello, @Model.Name";
    Models.User user = new Models.User() { Name = "Billy Boy" };
    RazorViewEngineRender view = new RazorViewEngineRender(Template);
    string Results = view.Render(user); //pass in your model
    

    Notes

    This only works with typed Models. I attempted to pass in a new { Name = “Billy Boy” } and it’s throwing errors. I’m not sure why and didn’t really look too deeply into it.

    This was fun, thanks for asking this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought I would use a stored routine to clean up some of my
I found this problem, which I thought would be interesting to solve but couldn't
I have an interesting thought-problem right now, so maybe someone of you could help.
This is what I thought would be a simple select clause, however the following
I'm being stymied by what I thought would be the relatively simple task of
I am trying to Emit what I thought would be a simple object array
I'm trying to do something which I thought would be fairly simple. Get IIS
Currently I'm trying to do what I thought would be a simple task: Draw
I am attempting to do what I originally thought would be pretty simple. To
I thought this would be fairly easy, but I'm totally baffled. I want one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.