How can I get the WebBrowser control to show modern contents?
Note: The post is about WebBrowser control, however, for all the new
.NET projects the main solution is using
WebView2.
To learn more, take a look at this post:
- Getting started with WebView2.
WebBrowser Control
The WebBrowser
control uses the same Internet Explorer version which is installed on your OS but it doesn't use the latest document mode by default and shows content in compatibility mode.
Symptom - As a symptom, the site works properly in Internet Explorer or other browsers, but WebBrowser
control doesn't show the site well and for some sites it shows script errors.
Solution - You can tell the WebBrowser
control to use the latest document mode without compatibility mode in WebBrowser
control. You can follow instructions here to disable the setting using registry.
[Reference: Browser Emulation]
Apply Browser Emulation setting using code
If you want to apply the settings using code, run the following code once:
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
true))
{
var app = System.IO.Path.GetFileName(Application.ExecutablePath);
key.SetValue(app, 11001, Microsoft.Win32.RegistryValueKind.DWord);
key.Close();
}
In above code, I've used 11001
which means IE11 Edge mode.
Internet Explorer 11. Webpages are displayed in IE11 edge mode,
regardless of the declared !DOCTYPE directive. Failing to declare a
!DOCTYPE directive causes the page to load in Quirks.
Apply the Browser Emulation setting manually
Open Registry editor and browse HKEY_CURRENT_USER
, go to the following key:
Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
Add the following values:
"YourApplicationFileName.exe"=dword:00002af9
"YourApplicationFileName.vshost.exe"=dword:00002af9
(In older versions of Visual Studio you needed to add vshost.exe value as well, when you run your program in Visual Studio.)
To create entries right click on an empty area of the right pane, then in the window which appears after selecting dword
value, choose hexadecimal and enter 2af9
:
In above steps, I've used 11001
which means IE11 Edge mode.
Use WebViewCompatible Control for Windows Forms
You can also use the new WebViewCompatible control for Windows Forms. You can see simple steps to use here: Replace WebBrowser control by new WebView Compatible control for Windows Forms.
WebViewCompatible
uses one of two rendering engines to support a broader set of Windows clients:
On Windows 10 devices, the newer Microsoft Edge rendering engine is used to embed a view that renders richly formatted HTML content from a remote web server, dynamically generated code, or content files.
On devices running older versions of Windows, the System.Windows.Controls.WebBrowser is used, which provides Internet Explorer engine-based rendering.
Note:
WebView2
is a replacement forWebView
andWebViewCompatible
.
Set X-UA-Compatibile meta tag
In case that you have access to the html content of the page and you can change the content (for example it's a local html file, or the site belong to yourself) then you can set X-UA-Compatibile
meta tag in the head
like: <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
.
Use other Browser Controls
You can rely on other browser controls like CefSharp
.
How to define which web browser will be used from the webBrowser control?
i am not sure if the article you posted refers to the same problem you have but i can explain what it says. The article exposes that the WebBrowser is using an olver version of IE to render the webpage and therefore it causes the error.
The solution given is to go into the windows registry, which you can do if you hit the Windows + R keys on your machine, opening a Run command and then typing in: regedit
This will open the windows registry Editor and in here you will see 4 groups of items, you will need to navigate to the one described on the article which is
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl
So first open the HKEY_LOCAL_MACHINE key, that will display further options, look for the SOFTWARE key now, this will display further options so continue this process untill you reach the last node FeatureControl and here look for the node called FEATURE_BROWSER_EMULATION.
If you do not find the Wow6432Node it means your PC is 32 bits in which case you would only have to modify this in HKEY LOCAL MACHINE > SOFTWARE > MICROSOFT > INTERNET EXPLORER > MAIN > FEATURE CONTROL > FEATURE_BROWSER_EMULATION
Then right click on the right side panel and click on the NEW option that displays from the menu and then DWORD, replace New Value #1 with the name of your application.exe and then for the value put 11000.
I hope this helps
Allow System.Windows.Forms.WebBrowser to run javascript
Set the property ScriptErrorsSuppressed
of the WebBrowser
control to true
to suppress the JavaScript error message.
In order to allow the code on hulu.com to execute, you must run the Webbrowser control in a mode such that it runs with newer version features. This can only be done by setting registry entries.
See this question and the answers for details.
To specify: I have a demo application to open the hulu website with the embedded WebBrowser control named WindowsFormsApplication5.exe.
Without registry changes, I see a note by Hulu that JavaScript support is not enabled. When sniffing the network transfer with Fiddler, I see that the following request is sent to the Hulu server:
GET http://www.hulu.com/ HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Accept-Language: de-DE,de;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Creative AutoUpdate v1.41.09)
Host: www.hulu.com
Connection: Keep-Alive
Pragma: no-cache
Note the version number "7.0" in the User-Agent string.
I now add a registry key of type REG_DWORD with name "WindowsFormsApplication5.exe" and value 0x00002af9 (11001) in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION.
As a result, the Hulu Website is successfully displayed in my demo application and I see the following request being sent via Fiddler:
GET http://www.hulu.com/ HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Referer: http://www.hulu.com/
Accept-Language: de-DE,de;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: www.hulu.com
Connection: Keep-Alive
Notice the different User-Agent string after the registry changes.
WinForms WebBrowser HtmlDocument.Write behaves differently in different solutions
The difference in behavior stems from the registry entry as proposed by Jimi and linked here
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
To reiterate, in the above registry the main program had an entry "ApplicationFileName.exe"=dword:00002af9
while my new test-application didn't.
This, in and for itself, does not explain the crashing of mshtml.dll itself but since the question was about the difference in behavior I'll post this as the answer. The crash is most likely linked to the outdated version used by Visual Studio but I haven't yet had the chance to look into some of the proposed fixes for that.
How can I get the WebBrowser control to show modern contents?
Note: The post is about WebBrowser control, however, for all the new
.NET projects the main solution is using
WebView2.
To learn more, take a look at this post:
- Getting started with WebView2.
WebBrowser Control
The WebBrowser
control uses the same Internet Explorer version which is installed on your OS but it doesn't use the latest document mode by default and shows content in compatibility mode.
Symptom - As a symptom, the site works properly in Internet Explorer or other browsers, but WebBrowser
control doesn't show the site well and for some sites it shows script errors.
Solution - You can tell the WebBrowser
control to use the latest document mode without compatibility mode in WebBrowser
control. You can follow instructions here to disable the setting using registry.
[Reference: Browser Emulation]
Apply Browser Emulation setting using code
If you want to apply the settings using code, run the following code once:
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
true))
{
var app = System.IO.Path.GetFileName(Application.ExecutablePath);
key.SetValue(app, 11001, Microsoft.Win32.RegistryValueKind.DWord);
key.Close();
}
In above code, I've used 11001
which means IE11 Edge mode.
Internet Explorer 11. Webpages are displayed in IE11 edge mode,
regardless of the declared !DOCTYPE directive. Failing to declare a
!DOCTYPE directive causes the page to load in Quirks.
Apply the Browser Emulation setting manually
Open Registry editor and browse HKEY_CURRENT_USER
, go to the following key:
Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
Add the following values:
"YourApplicationFileName.exe"=dword:00002af9
"YourApplicationFileName.vshost.exe"=dword:00002af9
(In older versions of Visual Studio you needed to add vshost.exe value as well, when you run your program in Visual Studio.)
To create entries right click on an empty area of the right pane, then in the window which appears after selecting dword
value, choose hexadecimal and enter 2af9
:
In above steps, I've used 11001
which means IE11 Edge mode.
Use WebViewCompatible Control for Windows Forms
You can also use the new WebViewCompatible control for Windows Forms. You can see simple steps to use here: Replace WebBrowser control by new WebView Compatible control for Windows Forms.
WebViewCompatible
uses one of two rendering engines to support a broader set of Windows clients:
On Windows 10 devices, the newer Microsoft Edge rendering engine is used to embed a view that renders richly formatted HTML content from a remote web server, dynamically generated code, or content files.
On devices running older versions of Windows, the System.Windows.Controls.WebBrowser is used, which provides Internet Explorer engine-based rendering.
Note:
WebView2
is a replacement forWebView
andWebViewCompatible
.
Set X-UA-Compatibile meta tag
In case that you have access to the html content of the page and you can change the content (for example it's a local html file, or the site belong to yourself) then you can set X-UA-Compatibile
meta tag in the head
like: <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
.
Use other Browser Controls
You can rely on other browser controls like CefSharp
.
Related Topics
Calling a Function from a String in C#
What Are the Differences Between Generics in C# and Java... and Templates in C++
How to Load Dll (Module Could Not Be Found Hresult: 0X8007007E)
How to Specify a [Dllimport] Path At Runtime
Passing Strings from C# to C++ Dll and Back - Minimal Example
Is Is Possible to Export Functions from a C# Dll Like in VS C++
How to Call a C# Library from Native C++ (Using C++\Cli and Ijw)
Reach Control from Another Page. Asp.Net
How to Dynamically Generate HTML Code Using .Net'S Webbrowser or Mshtml.Htmldocument
Convert Webpage to Image from Asp.Net
How to Find the Text Within a Div in the Source of a Web Page Using C#
How to Convert HTML to Plain Text