5

I am experimenting in MVC 4, and i was wandering if it is possible to turn a string into a View and return this from the Controller.

For instance, i have a method that reads the View contents into a string. I would like to process that string before rendering it. So i would like the Controller to output the modified string as a View.

Is this even possible?

Just to make a note, this will happen after Razor has made any changes. I do have a method that gets the final View into a string.

Edit

I have the following method:

    public static string RenderViewToString(this Controller controller, string viewName, object model)
    {
        controller.ViewData.Model = model;
        try
        {
            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, null);
                ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);



                return sw.ToString();
            }
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

which returns the finally rendered View (what is actually going to be displayed in the browser) into a string.

I need to do the opposite. Having a string, output a View.

So if i have the following cshtml:

<!doctype html>
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
    Hello World
</body>
</html>

The above function will return a string like:

"<!doctype html><head><title>MyActualTitle</title></head><body>Hello World</body></html>"

Now what i want is take this string, manipulate it and render it. For instance i want to change the Hello World. I know i can do this with Razor, i was just wandering if i can do this with another method.

leppie
  • 115,091
  • 17
  • 196
  • 297
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
  • Unclear. You want the controller to process something _after_ Razor? And what sort os string, what sort of View? – H H Oct 07 '13 at 10:09
  • I am really at the point of getting the View into a string and i wanted to do the other way. – Giannis Paraskevopoulos Oct 07 '13 at 10:13
  • So, you want to render a view to a string.. modify that string.. then render the new string as a view? – Simon Whitehead Oct 07 '13 at 10:14
  • 1
    I think the missing link here is what is actually in the string... is it a razor view or is it standard HTML - if it's standard HTML all you need really is `@Html.Raw("

    My HTML

    ");`
    – James Oct 07 '13 at 10:16
  • 1
    Ok, I know I probably won't understand, but WHY would you try to violate MVC like this? – walther Oct 07 '13 at 10:22
  • What you are doing is breaking the MVC architecture, don't create a View in a controller and spit it out as a string! create the view in the View!, use a ViewModel to pass info from controller to View... look into using ViewModels – Paul Zahra Oct 07 '13 at 10:30
  • It is just experimenting. The idea is having the content translated, so it won't actually break any of the View, so it is still going to make MVC sense. Or so i think. – Giannis Paraskevopoulos Oct 07 '13 at 10:36

3 Answers3

3

Yes is possible:

public sealed class StringController : Controller
{
    //
    // GET: /String/
    public string Index()
    {
        return "Hello world!";
    }
}

If you want razor to process it before returning, then I'm not sure how to do that, but you probably don't need to go via a View to do that.

weston
  • 54,145
  • 21
  • 145
  • 203
2

If you need to create a view form string .Then see this discussions

Render a view as a string

ASP.NET MVC3 Razor - create view from a string?

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
1

You could just use Content for this.

public ActionResult Index()
{
      return Content("Some string output");
}