Skip to content

Local and External Resource Mapping

mattkol edited this page Jul 21, 2021 · 2 revisions

Expectedly most developers would use Host To Folder Mapping to map local resources, but in rare situations this pre-built functionality may not be enough. For those kinds of situations, EdgeSharp allows registering resource handlers. The handlers can be used for local resources mapping (local to the the OS or within the same same OS network) and external resources e.g contained within an assembly.

A resource handler must implement IResourceRequestSchemeHandler and registered.

To register a resource handler, the devloper can either use the default handler or create a custom one.

To use the default handler, no custom scheme needs registered, but the scheme and associated host must be registered/added in UrlSchemes

To register a resource Scheme, it requires

  • Scheme - e.g http, https
  • Host - e.g app, myFolder - relative to the application folder (where the exe is).
  • Folder - null. It is redundant - unused
  • Scheme type - ResourceRequest. Other scheme types can be found at: UrlSchemeType.

Following will use:

Scheme Host Folder Scheme Type Index or Default File
http app null ResourceRequest index.html
var config = new Configuration();
var localResource = new UrlScheme("http", "app", null, UrlSchemeType.ResourceRequest);
config.UrlSchemes.Add(localResource);
config.StartUrl = "http://app/index.html";

The default handler is suitable for local folders relative to the excutable location, for other sitautions like mapping networked folders, or resources in an assembly files (.dll), it is advisable to use a custom resource handler.

    1. Create the custom resource scheme handler
public class CustomResourceSchemeHandler : IResourceRequestSchemeHandler
{
    public UrlScheme UrlScheme 
    { 
	get { return new UrlScheme("http", "customFolder", null, UrlSchemeType.ResourceRequest); }
    }
	
    public virtual void ProcessRequest(IRequest request, CoreWebView2Deferral deferral, Action<IResponse, CoreWebView2Deferral> 
    callback)
     {
		---
     }
}
    1. Register the custom resource handler
class SampleSharpApp : EdgeSharpBasicApp
{
    public override void ConfigureServices(IServiceCollection services)
    {
        base.ConfigureServices(services);
	services.TryAddSingleton<IResourceRequestSchemeHandler, CustomResourceSchemeHandler>();
        // other service configuration can be placed here
    }
}