ASP Page becoming truncated when converting to IHTMLDocument2 using .innerHTML - c#

I'm reading in an .ASP page from a server. The problem I am having is that the page is becoming truncated when I attempted to read the data in by means of the C# code below.
Below is my code accessing the .asp page
var htmlDocument = EmbeddedBrowser.Document as IHTMLDocument2;
var htmlInnerContent = (((HTMLDocument)(htmlDocument)).documentElement).innerHTML;
Below is the .asp page as it sits on my server
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script type="text/javascript">Lots of Java Script Here</script><script type="text/javascript">Lots more Java Script Here</script>
</head>
<body>
<input type="" name="ExpectedClientVersion" value="20.15.09"/>
</body>
</html>
Below is the content of htmlInnerContent after I read it in
<head>
<script type="text/javascript">Lots of Java Script Here</script
</head>
As you can see I am missing the body which is really what I need so I can parse the ExpectedClientVersion.
I have run a fiddler trace and I can see the entire document being passed over.
I also tried researching limitations of .InnerHTML but found only lacking documentation. There may be something to this.
I feel the way I am accessing the document may be incorrect. Does anyone have insight into this?

Related

Format HTML with C#

I want to format html code with C#.
From this:
<!DOCTYPE html><html><head><title>Hello World!</title></head><body><h1>Hi, HTML!</h1></body></html>
I want this:
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hi, HTML!</h1>
</body>
</html>
I tried:
string result = System.Xml.Linq.XElement.Parse(source).ToString();
And this works, but if I try this with Google website code, it throws some exceptions because
doctypeisn't written correctly.
How can I format HTML code without HTML syntax error check?
Can anyone help me? Thanks in advance!

How can i retrieve the value of this element and assert it after a change is done?

I am working on automating an area of a web page (not able to provide the webpage as the contents are confidential, although will try to give as much insight as possible).
This element has on it an html code preview that will change after some selections are done. Here is the page html of the element:
<div _ngcontent-hje-c241="" class="field" style="position: relative;">
<pre _ngcontent-hje-c241="" class="code-pre">
"
<!doctype html>
<html>
<head>
<meta charset='utf'>
<title></title>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' />
<style type='text/css'>body,html, #video {height:100%;margin:0;overflow:hidden;background-color:#000;}</style>
<script type='text/javascript'>
window.onload = function(event) {
var config = {containerID: 'video', Player: true, wmode: 'direct'};
myplayer = new Test.embed('WmAl', config);
}
</script>
</head>
<body>
<div id='video'></div>
</body>
</html>"
(I have edited and removed any confidentials parts of the string for the html, the html itself was not changed.)
I would need to get the value of the element I found through class="code-pre".
Here is what I have tried:
IWebElement htmlTest = driver.FindElement(By.ClassName("code-pre"));
var defaultHtmlTestValue = htmlTest.GetAttribute("value");
Assert.IsFalse(htmlTest.Equals(defaultHtmlTestValue), "The html has not changed after the Http selection");
The assert passes, altho, i would like to see what is the value that is being taken, as i feel like is not taking the html example i am trying to get.
I have also used Debug.Writeline(htmlTest) to see if it worked, but i got "Internal error in the expression evaluator". This is also an issue i will be trying to fix.
I am quite new to automation and stack overflow. Please let me know if there is a way i can improve this post.
I haven't worked with the html previews but looks like the whole html code is just the inner text of an element with class code-pre.
try doing :
String htmlTest = driver.FindElement(By.ClassName("code-pre")).getTex();
System.out.println(htmlTest);
Assert.assertTrue(htmlTest.Equals(defaultHtmlTestValue), "The html has not changed after the Http selection");
You can easily convert above java code into your language.

Insert JavaScript to Head Tag After Page is Almost Finished

Is there a way using a web form in ASP.NET to insert a script reference to the head tag once the page has almost finished Page_Load?
For example:
<%# Page Language="C#"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<!-- generate script about content here --->
</head>
<body>
<!-- generate content here --->
</body>
</html>
<%
//Write script about content, insert to head tag.
%>
I found a way to do this that works with both web forms and MVC.
Serialize an instance of a class using Newtonsoft and then store the JSON in the memory cache. Then, when a new instance of the class is created, pull the JSON out of the cache and initialize the instance with it. Last, I created a web form that pulls out the cached copy to generate JavaScript that is then referenced in the head tag.

IIS aways returns an empty page

I am deploying a site built as a set of projects in a solution which is deployed as a series of websites (one project = one url).
The coding lanague is c# and the site uses a mssql db accessed via linq.
Everything works ok on the development server but at the moment, all pages with dynamically created elements (i.e all <asp> and custom tags) output a blank page with the following source code. Static pages (standard html and aspx) display correctly.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY></BODY></HTML>
I normally work with php on LAMP and this behaviour is unlike anything I've seen in that setup. I need some fresh ideas on causes / resolution of this error (preferably step by step / tutorial links - like I say IIS/c#.net is not my normal enviroment).
Things I have tried with no effect whatsoever:
altering permissions on site folders
altering web.config settings
Redeploying site files via copy / paste and the vb publish option with various settings
Plus a whole ton (over two days so far) of internet research
Thanks for all replies.
edit:
version numbers:
iis: v6.1 (bulid: 7601)
OS: Windows web server 2008 R2 / Service pack 1
.net: v4
second edit:
Sample page:
aspx file:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="dbtest.aspx.cs" Inherits="Rica.Yoodul.dbtest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>test</h1>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace Rica.Yoodul
{
public partial class dbtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RicaConnectionString"].ConnectionString);
myConnection.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT * FROM dbo.MaritialStatus", myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Literal1.Text = Literal1.Text + myReader["Name"].ToString();
}
myConnection.Close();
}
}
}
edit:
Something I didn't add: Based on some of the other behaviour the server is displaying, I think the most likely culprit is misconfiguration of the server.
edit:
OK I've narrowed down the cause a little. By taking out everything except the langauge attribute in the first line of the front end file I can get the server to display the static content.#
edit:
OK http://www.iis.net/ConfigReference covers the use of the iis config files. Only I went to the path indicated on the page and I don't appear to have any config files at all. How do I comfirm / fix this?
It sounds like your production server doesn't have ASP.NET correctly installed/registered, so your pages are being treated as simple text documents rather than code. This can sometimes happen if you install .NET before installing IIS, for example.
To confirm this, put together a barebones page that includes a mix of HTML and ASP.NET...
<%# Page Language="C#" %>
<html>
<head>
<title>ASP.NET Hello World</title>
</head>
<body>
<p>Hello from HTML</p>
<p><%= "Hello from ASP.NET" %></p>
</body>
</html>
If you load that up and only get the plain-HTML greeting, use your browser's "View Source" option and see what you're actually getting. If your ASP.NET source-code is shown in the source the browser's receiving, that confirms that IIS didn't treat it as an ASP.NET file - which means ASP.NET isn't installed correctly or is disabled.

Loading complete Html-page in contentpane of aspx-page

here at home I have different projects and libraries for which I've created an helpfile with Sandcastle.
Now Sandcastle provides also the possibility to create a website.
What I would like to do is to create an aspx-page where I can dynamically create a menu and where the existing helpfile-websites can be sollicited. All in one place.
Is it possible to accomplish this? Maybe some control that I can use to view an entire webpage?
Thank you.
EDIT:
Seems I can't get it to work in an ASP-page for whatever reason, but probably because of the way Sandcastle creates the help-pages.
I've now tried it with a WinForm-application with a webbrowser-control and this approach works, so I guess this will be the way I have to go here.
But I do need to say thanks to Alison (and Leon) for their help regarding this issue.
Their solution works for "normal" html-pages, but (unfortunately) not for the ones I have.
For that reason, I've accepted the answer so others could benefit from it.
Updated
Take a look at jQuery load. You can have a div on your page and load the html from an external page into it. The load function can grab individual pieces of HTML from a different page.
On your main page, add this html:
<div id="myexternalpage"></div>
On the different page, add a div tag with an id around the content you want to grab like:
<div id="myexternalcontent">Test</div>
The add the following to your head tag:
<script type="text/javascript">
$(document).ready(function() {
$('#myexternalpage').load('myexternalpage.html #content');
})
</script>
Notice the addition of the "#content" selector to the end of the load function? This will have jQuery load the different page and return only the content in the div with id="content".
Using jQuery load will let you load the content once the page loads and you won't need to use any iFrames. You can use CSS to handle the height/width and handle any overflow.
I've made a quick and dirty test-page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Test2.aspx.cs" Inherits="Test2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js " ></script>
<script type="text/javascript">
$(document).ready(function () { $('#myexternalpage').load('WebHelp/KoenHoefman/ExhangeRateWebService/Index.html'); })
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="myexternalpage">
</div>
</form>
</body>
</html>
But I get an error on the script line.
Microsoft JScript runtime error: Object expected
Do I have to replace document with something (probably a stupid question but I'm not used to use javascript)?
Update:
Added Leon's code and now it shows something. But when I want to use the page I really want to show (which is located in a subfolder) I get only the items on the index.html. The page that should be loaded into the IFrame of that page isn't shown. Also the pictures that are located in the same folder are not shown.
Error: HTTP 404
Requested URL:
/html/aea04102-3d0d-cf0d-f5f4-5634f5f06aed.htm

Categories