0

I have the following code in my old MVC project. I am trying to migrate it to Asp.Net Core. Anyone have the idea about how a new HttpResponse so that I can migrate the following code ?

 private string RenderActionResultToString(ActionResult result)
        {
            // Create memory writer.
            var sb = new System.Text.StringBuilder();
            var memWriter = new System.IO.StringWriter(sb);

        // Create fake http context to render the view.
        var fakeResponse = new HttpResponse(memWriter);
        var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request,
            fakeResponse);
        var fakeControllerContext = new ControllerContext(
            new HttpContextWrapper(fakeContext),
            this.ControllerContext.RouteData,
            this.ControllerContext.Controller);
        var oldContext = System.Web.HttpContext.Current;
        System.Web.HttpContext.Current = fakeContext;

        // Render the view.
        result.ExecuteResult(fakeControllerContext);

        // Restore old context.
        System.Web.HttpContext.Current = oldContext;

        // Flush memory and return output.
        memWriter.Flush();
        return sb.ToString();
    }`
1
  • Why would you want to do something like "fake" controller and response? Just curious ... Commented Jan 7, 2017 at 9:10

1 Answer 1

-1

I think the analogous code for what you are trying to do can be found in the answer to this StackOverflow Question: Render Razor view to string in ASP.NET 5

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.