1

I have a simple C# app using the cefSharp chromium browser that I am loading a webpage with. Problem is a mailto link does not redirect to the default mail client. I have been reading and it appears that I need to add a handler. Documentation says to use the OnBeforeResourceLoad. I have tried every way I can think of but cant get it to work. I was hoping someone could add to the code I have here that would include the class (in the proper location and and anything else to get it to work. Here is a link that I was trying to use the information, but don't know how to implement it.

How to open a link in a native browser from CefSharp 3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;    

namespace WindowsFormsApp4
{    
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            InitBrowser();            
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public ChromiumWebBrowser browser;
        public void InitBrowser()
        {
            Cef.Initialize(new CefSettings());
            browser = new ChromiumWebBrowser("https://google.com");

            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
        }        
    }
}

Here is one of the ways I was trying but could not get to work.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;


namespace WindowsFormsApp4
{
    public class BrowserRequestHandler : IRequestHandler
    {
        public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
        {
            // Open in Default browser
            if (!request.Url.StartsWith("mailto:"))
            {
                System.Diagnostics.Process.Start(request.Url);
                return true;
            }
            return false;
        }
    }
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            InitBrowser();            
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public ChromiumWebBrowser browser;
        public void InitBrowser()
        {
            Cef.Initialize(new CefSettings());
            browser = new ChromiumWebBrowser("https://google.com");
            browser.RequestHandler = new BrowserRequestHandler();
            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
        }        
    }
}

I added

using CefSharp.Handler;

and now I do not get an error but it does not even pop up the message box. This is what I have:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;

using CefSharp.Handler;

namespace CefSharp
{

}

namespace WindowsFormsApp4
{
    public class CustomResourceRequestHandler : ResourceRequestHandler
    {
        public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
        {
            System.Windows.Forms.MessageBox.Show("Test");
            // Open in Default browser
            if (!request.Url.StartsWith("mailto:"))
            {
                System.Diagnostics.Process.Start(request.Url);
                return true;
            }
            return false;
        }

    }

    public class CustomRequestHandler : RequestHandler
    {
        protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
        {
            return new CustomResourceRequestHandler();
        }
    }

    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            InitBrowser();            
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public ChromiumWebBrowser browser;
        public void InitBrowser()
        {
            Cef.Initialize(new CefSettings());
            browser = new ChromiumWebBrowser("https://google.com");
            browser.RequestHandler = new CustomRequestHandler();
            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
        }        
    }
}

I got it to work, but I would like your input if it I am doing this correctly.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using CefSharp.Handler;



namespace WindowsFormsApp4
{
    public class CustomResourceRequestHandler : ResourceRequestHandler
    {


        protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
        {
            if (request.Url.StartsWith("mailto:"))
            {
                System.Diagnostics.Process.Start(request.Url);
                return CefReturnValue.Cancel;
            }
            return CefReturnValue.Continue;
        }

    }

    public class CustomRequestHandler : RequestHandler
    {
        protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
        {
            return new CustomResourceRequestHandler();
        }
    }

    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            InitBrowser();            
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public ChromiumWebBrowser browser;
        public void InitBrowser()
        {
            Cef.Initialize(new CefSettings());
            browser = new ChromiumWebBrowser("http://www.google.com");
            browser.RequestHandler = new CustomRequestHandler();
            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
        }        
    }
}
jrking1978
  • 11
  • 2
  • 1
    `I have tried every way I can think of but cant get it to work.` Why don't you show us any of these attempts? Generally, when someone says "I've tried *everything!*" but they don't show us *anything*, that usually means they didn't actually try much. –  Nov 08 '19 at 21:54
  • 1
    See https://stackoverflow.com/a/31367169/4583726 for reference, instead of `OnBeforeResourceLoad` you should use http://cefsharp.github.io/api/75.1.x/html/M_CefSharp_IResourceRequestHandler_OnProtocolExecution.htm instead of `OnBeforeResourceLoad` see https://github.com/cefsharp/CefSharp/blob/802bb7f43c33626c28975ea958777daff0f2e9e1/CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs#L107 – amaitland Nov 08 '19 at 22:09
  • Sorry, I am new to C#. So I am sure I have some things in wrong places. I added one of the ways I tried. One of the errors I get is BrowserRequestHandler' does not implement interface member 'IRequestHandler.OnBeforeBrowse(IWebBrowser, IBrowser, IFrame, IRequest, bool, bool) – jrking1978 Nov 08 '19 at 23:08

0 Answers0