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 7573963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:18:53+00:00 2026-05-30T16:18:53+00:00

I need to acquire HTML markup of a controller/action in order to generate PDF.

  • 0

I need to acquire HTML markup of a controller/action in order to generate PDF. What I have done is:

    public ActionResult Index()
    {
        Session["Message"] = "SESSION-MESSAGE";

        String URL = "http://localhost:7401/Home/SuperComplex";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
        req.CookieContainer = new CookieContainer();

        for (int i = 0; i <= this.Request.Cookies.Count - 1; i++)
            req.CookieContainer.Add(
                new System.Net.Cookie(
                    name: this.Request.Cookies[i].Name,
                    value: Request.Cookies[i].Value,
                    path: Request.Cookies[i].Path, domain: this.HttpContext.Request.Url.Host)
                );

        using (var r = req.GetResponse())
        {
            using (var s = new StreamReader(r.GetResponseStream()))
            {
                var htmlToPrint = s.ReadToEnd();
                Response.Write("<h1>" + htmlToPrint + "</h1>");
            }
        }

        return View();
    }

Considering above said situation, in SuperComplex session, I should have the Session[“Message”]. But for some strange reason, it does not go there.

I have checked Session.SessionId – in both cases it is same.

Also, on second or third request, request timesout!

Again: http://localhost:7401/(S(SESSION_ID))/Home/About

If requested in other browser: session hijack does happen – but WebRequest dies! 🙁

Help – anyone?

  • 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-30T16:18:54+00:00Added an answer on May 30, 2026 at 4:18 pm

    Store your HTML in a Partial View and then use a Helper function to parse it into a string.

    // usage
    /*
     * http://stackoverflow.com/questions/4344533/asp-net-mvc-razor-how-to-render-a-razor-partial-views-html-inside-the-controll
     * 
        var model = _repository.Find(x => x.PropertyID > 3).FirstOrDefault();
        var test = this.RenderViewToString("DataModel", model);
        return Content(test);
     */
    
    public static string RenderPartialToString<T>(this ControllerBase controller, string partialName, T model)
    {
        var vd = new ViewDataDictionary(controller.ViewData);
        var vp = new ViewPage
        {
            ViewData = vd,
            ViewContext = new ViewContext(),
            Url = new UrlHelper(controller.ControllerContext.RequestContext)
        };
    
        ViewEngineResult result = ViewEngines
                                  .Engines
                                  .FindPartialView(controller.ControllerContext, partialName);
    
        if (result.View == null)
        {
            throw new InvalidOperationException(
            string.Format("The partial view '{0}' could not be found", partialName));
        }
        var partialPath = ((RazorView)result.View).ViewPath;
    
        vp.ViewData.Model = model;
    
        using(StringWriter sw = new StringWriter()) {
            ViewContext viewContext = new ViewContext(controller.ControllerContext, result.View, vd, controller.TempData, sw);
            result.View.Render(viewContext, sw);
    
            return sw.GetStringBuilder().ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a use case where we need to acquire a lock and send
If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make
need ask you about some help. I have web app running in Net 2.0.
I've got the following problem: I have rhtml (html minced together with ruby inside
I have a device to acquire XRay images. Due to some technical constrains, the
Does a game need to have levels? Does having no levels put people off?
Suppose you have three objects you acquire via context manager, for instance A lock,
Here's my dilemma. I have 2 animations that need to run sequentially. The first
I need clarification regarding an issue related to multithreading. I have threads which acquires
I just need a confirmation that i have understood the concept of locks in

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.