0

I am working on a CefSharp application in which we are trying to load content locally. It's partially working for me, as it's only loading HTML. Any type of CSS, JS, images are not loaded. I have tried adding multiple paths, even tried directly adding it in the same resources folder, but it doesn't work. What am I doing wrong?

Code :

namespace OurApp
{
    public partial class OurApp : Form
    {
        public ChromiumWebBrowser chromiumBrowser;
        FileResourceHandlerFactory fileResourceHandlerFactory = new FileResourceHandlerFactory("app","local","resources","test.html");
        public OurApp()
        {
            InitializeComponent();
            CefSettings settings = new CefSettings();

            settings.RegisterScheme(new CefCustomScheme
            {
                SchemeName = "app",
                SchemeHandlerFactory = fileResourceHandlerFactory,
                IsSecure = false
            });
            settings.UserAgent = "app,win";
            settings.PersistSessionCookies = true;
               Cef.Initialize(settings);
            InitializeChromium();
            chromiumBrowser.TitleChanged += OnBrowserTitleChanged;
        }
        private void InitializeChromium()
        {
            chromiumBrowser = new ChromiumWebBrowser(string.Empty)
            {
                Dock = DockStyle.Fill,
            };
            chromiumBrowser.Size = new Size(1080, 1920);

            string curDir = Directory.GetCurrentDirectory();
            chromiumBrowser.Load("app://local");

       }


//Resource handler : 

 public class FileResourceHandlerFactory : ISchemeHandlerFactory
    {
        private string scheme, host, folder, default_filename;

        public string Scheme => scheme;

        public FileResourceHandlerFactory(string scheme, string host, string folder, string default_filename = "test.html")
        {
            this.scheme = scheme;
            this.host = host;
            this.folder = folder;
            this.default_filename = default_filename;
        }

        private string get_content(Uri uri, out string extension)
        {
            var path = uri.LocalPath.Substring(1);
            path = string.IsNullOrWhiteSpace(path) ? this.default_filename : path;
            extension = Path.GetExtension(path);
            return File.ReadAllText(Path.Combine(this.folder, path));
        }

        IResourceHandler ISchemeHandlerFactory.Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
        {
            var uri = new Uri(request.Url);
            return ResourceHandler.FromString(get_content(uri, out var extension), extension);
        }
    }
}

Thank you.

We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • One problem is that you are not returning a mime type (otherwise known as *Content-Type*). It also sounds like you are using `FromString()` and `ReadAllText()` for all files, including images. This is incorrect since images are binary. Moreover, you did not specify what is shown in the debugger window of your Web Browser for the *Networking*. Furthermore, you are using the approach from *Luis Perez*, not [mine](https://stackoverflow.com/a/39560549/4478484). Also see a [reference implementation](https://github.com/cefsharp/CefSharp/blob/master/CefSharp/ResourceHandler.cs). – Michael Aug 28 '18 at 21:00
  • You can use the built in `FolderSchemeHandlerFactory` see http://cefsharp.github.io/api/63.0.0/html/T_CefSharp_SchemeHandler_FolderSchemeHandlerFactory.htm – amaitland Sep 03 '18 at 05:24

0 Answers0