0

I am trying to get the html body of the view on post but my body is always empty. below is my code.

[HttpPost]
public async Task<IActionResult> GetPdf()
        {

            var request = HttpContext.Request.Body;
            using (var bodyReader = new StreamReader(request))
            {
                string body = await bodyReader.ReadToEndAsync();
                //request.Body = new MemoryStream(Encoding.UTF8.GetBytes(body));
            }
        }
maxspan
  • 13,326
  • 15
  • 75
  • 104
  • A form only posts back the name/value pairs if its successful form controls. It does not post back any html –  Jan 29 '18 at 03:51
  • How can I get the html body – maxspan Jan 29 '18 at 03:54
  • Try creating an object with a string property having value html body and send it to the route by json serializing it. Use this to get the object public async Task GetPdf(object obj) – Subash Kharel Jan 29 '18 at 04:00
  • You would have to use jquery to get the html and assign it to an input in the form - e.g. `$('#yourInput').val($('body').html());` –  Jan 29 '18 at 04:02
  • Refer also [this article](http://www.c-sharpcorner.com/blogs/convert-html-to-pdf-in-asp-net-mvc-razor1) for an alternative –  Jan 29 '18 at 04:04
  • @maxspan Why do you want to do this? – Worthy7 Jan 30 '18 at 01:15
  • I just want to convert html string to pdf in asp.net core 2.0. – maxspan Jan 30 '18 at 01:27

1 Answers1

0

Post request contains only form fields (if it is HTML form post) or values explicitly added to request (if it is AJAX / other manual post).

If you really need to get Html of the page that way - grab whole HTML (i.e. with jQuery's $('body').html()) and post it back to server (i.e. How to pass parameters in $ajax POST?).

 $.post('myServerUrl', { html: $('body').html()}, ...} 

Note that if you trying to do something like rendering of HTML to PDF you'd also need CSS and possibly JS files. Using more specialized tools like HTML to PDF converters may be more appropriate.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179