0

Example --

var myView = "<p>The date is @DateTime.Now.ToLongDateString()</p>";
return View(myView);

Essentially, I want to override the MVC view resolution functionality which looks for the view on the file system. Instead of reading the Razor codein from a file, I want to provide it as a string.

(Why? So I can compile Views in a separate DLL and drop them into projects. Let me know if I'm going about this the completely wrong way.)

Deane
  • 8,269
  • 12
  • 58
  • 108
  • http://stackoverflow.com/questions/4730777/mvc-return-partial-view-as-json – Matt Bodily Jan 18 '14 at 03:04
  • That's the exact opposite of what I want. I don't want to RENDER a view as a string, I want to PROVIDE a view as a string. – Deane Jan 18 '14 at 03:31
  • so the answer to your question is yes. the link I gave turns a view into a string. Look at the format and you can create your string views as the same format and render them on the view http://stackoverflow.com/questions/18667447/return-partial-view-and-json-from-asp-net-mvc-action – Matt Bodily Jan 18 '14 at 03:38

2 Answers2

0

You can use RazorEngine to parse Razor from a string:

    string result = RazorEngine.Razor.Parse("<p>The date is @DateTime.Now.ToLongDateString()</p>");
    return Content(result);
Saravana
  • 37,852
  • 18
  • 100
  • 108
0

Controller class has one method named content which returns ContentResult

in your action you can use it like Content("your string content");

to your content you can configure this things

        // Summary:
        //     Gets or sets the content.
        //
        // Returns:
        //     The content.
        public string Content { get; set; }
        //
        // Summary:
        //     Gets or sets the content encoding.
        //
        // Returns:
        //     The content encoding.
        public Encoding ContentEncoding { get; set; }
        //
        // Summary:
        //     Gets or sets the type of the content.
        //
        // Returns:
        //     The type of the content.
        public string ContentType { get; set; }
Vishal Sharma
  • 2,773
  • 2
  • 24
  • 36