4

My purpose of this is to render string into view. This is what i have done:

public string RenderRazorViewToString(string viewName, object model, string controllerName)
{
    ViewData.Model = model;
    Type controllerType = Type.GetType(controllerName + "Controller");
    object controller = Activator.CreateInstance(controllerType);
    RouteData rd = new System.Web.Routing.RouteData();
    rd.Values.Add("controller", "Account");
    ((ControllerBase)(controller)).ControllerContext = new ControllerContext(HttpContext, rd, (ControllerBase)controller);
    ControllerContext cc = ((ControllerBase)(controller)).ControllerContext;

    using (var sw = new StringWriter())
    {
        var viewResult = ViewEngines.Engines.FindView(cc, viewName, null);
        var viewContext = new ViewContext(cc, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);
        viewResult.ViewEngine.ReleaseView(cc, viewResult.View);
        return sw.GetStringBuilder().ToString();
    }
}

Basically what this code does is to render a view file that is found in :

var viewResult = ViewEngines.Engines.FindView(cc, viewName, null);

And renders it in :

viewResult.View.Render(viewContext, sw);

What i want is not finding view file, but i want to render view that is stored in my database as string. So what i need is:

  1. Getting database view string
  2. Render the database view string
  3. Output it as string after render

How can i achieve this?

Alvin Stefanus
  • 1,873
  • 2
  • 22
  • 60
  • Can you explain why the other similar questions already asked on StackOverflow don't apply to your case? (e.g. http://stackoverflow.com/questions/19922936/render-razor-from-database-or-other-sources or http://stackoverflow.com/questions/4218454/asp-net-mvc-load-razor-view-from-database) – StriplingWarrior Nov 09 '15 at 23:23
  • 2
    Indeed, i have tried both solutions, but my case is different. I have tried RazorEngine, but the drawback of razor engine is that it cannot resolve html helpers and url helpers for mvc view. Also with virtualpathprovider, i dont want the whole routing system to be changed into virtual. I still want to use my routing system, but with VPP, the system is forced to follow VPP on every routing because VPP is assigned in global.asax. So both solutions do not work. I am guessing VPP may be work, but only if i can implement VPP only on my spesific controller, but i do not know how. – Alvin Stefanus Nov 10 '15 at 03:38
  • There does [appear to be a way](http://stackoverflow.com/a/19434112/120955) to make RazorEngine support Html Helpers and such. Maybe that'll work for you. – StriplingWarrior Nov 10 '15 at 16:24
  • Using RazorEngine to support MVC is so complicated, i have tried a lot. Your suggestion is greatly appreciated, but i am thinking about the future terms, when i need to use another helper like TokenValidation, etc. Also there is a syntax error when i write @{variable = " values "} after @model Model.ThisModel. So i dont think using RazorEngine is reliable. – Alvin Stefanus Nov 11 '15 at 03:53
  • If you're not willing to try to get RazorEngine to work for this, then you're next best bet would probably be to create a VirtualPathProvider that uses a specific path signature to indicate that you're trying to pull from the database. It would exit immediately for any path that didn't match that signature, so it wouldn't be a big deal for it to be registered globally. MVC will cycle through all of the registered VirtualPathProviders, so your path provider could just be a fallback to the regular one. – StriplingWarrior Nov 11 '15 at 16:17
  • Thank you for the suggestion stripling, yeah im working on it with VPP. Is there any example that the routing runs on both routing? (VPP and normal routing) that i can follow? – Alvin Stefanus Nov 12 '15 at 02:55
  • Well, [this guy](http://www.umbraworks.net/bl0g/rebuildall/2009/11/17/ASP_NET_MVC_and_virtual_views) just makes his implementation call into the `base` method so that it will fall back to the default implementation, but the documentation also seems to indicate that MVC will cycle through all the registered path providers until the file is found, so it seems like it should work without doing that. But I'm not in a position to test it. – StriplingWarrior Nov 12 '15 at 16:06

1 Answers1

-2

You can simply use

@Html.Raw(mystring)

mystring will be the view content stored in database

Ravi
  • 316
  • 7
  • 17
  • 1
    This wont work, because Raw() only display the string without rendering it. I need all the helpers such as Html.BeginForm, Url.Action, etc.. to be translated first into html. – Alvin Stefanus Nov 10 '15 at 09:28