Click or drag to resize

DavEngineRegisterMethodHandler Method

IT Hit WebDAV Classes Reference
Registers custom method handler.

Namespace: ITHit.WebDAV.Server
Assembly: ITHit.WebDAV.Server (in ITHit.WebDAV.Server.dll) Version: 4.5.3121.0
public IMethodHandler RegisterMethodHandler(
	string method,
	IMethodHandler handler


Type: SystemString
HTTP verb.
Type: ITHit.WebDAV.Server.ExtensibilityIMethodHandler
Custom handled implementing IMethodHandler interface.

Return Value

Type: IMethodHandler
Original handler if any.
Using this method you can register custom method handler to be called by the engine. If the handler for the specified method was already defined it is returned from this method. The original handler can be saved and called later from your custom handler.
DavEngine engine = new DavEngine();
MyCustomGetHandler handler = new MyCustomGetHandler();
handler.OriginalHandler = engine.RegisterMethodHandler("GET", handler);

MyDavContext context = new MyDavContext(...);

The code below is part of 'CardDAVServer.NtfsStorage' sample provided with the SDK.

internal class MyCustomGetHandler : IMethodHandler
    public IMethodHandler OriginalHandler { get; set; }

    public bool EnableOutputBuffering
        get { return false; }

    public bool EnableOutputDebugLogging
        get { return false; }

    public bool EnableInputDebugLogging
        get { return false; }

    public void ProcessRequest(DavContextBase context, IHierarchyItem item)
        string htmlPath = HttpContext.Current.Request.MapPath("/");

        if (item is IItemCollection)
            // In case of GET requests to WebDAV folders we serve a web page to display 
            // any information about this server and how to use it.

            // Remember to call EnsureBeforeResponseWasCalled here if your context implementation
            // makes some useful things in BeforeResponseAsync.

            // Request to iOS/OS X CalDAV/CardDAV profile.
            if (context.Request.RawUrl.EndsWith("?connect"))
                WriteProfile(context, item, htmlPath);
            IHttpAsyncHandler page = (IHttpAsyncHandler)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(
                "~/MyCustomHandlerPage.aspx", typeof(MyCustomHandlerPage));
            OriginalHandler.ProcessRequest(context, item);

    public bool AppliesTo(IHierarchyItem item)
        return item is IFolder || OriginalHandler.AppliesTo(item);

    private void WriteProfile(DavContextBase context, IHierarchyItem item, string htmlPath)
        string mobileconfigFileName = null;
        string decription = null;
        if (item is IAddressbookFolder)
            mobileconfigFileName = "CardDAV.AppleProfileTemplete.mobileconfig";
            decription = (item as IAddressbookFolder).AddressbookDescription;

        decription = !string.IsNullOrEmpty(decription) ? decription : item.Name;

        string templateContent = null;
        using (TextReader reader = new StreamReader(Path.Combine(htmlPath, mobileconfigFileName)))
            templateContent = reader.ReadToEnd();

        Uri url = new Uri(context.Request.UrlPrefix);

        string payloadUUID = item.Path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last(); // PayloadUUID

        string profile = string.Format(templateContent
            , url.Host // host name
            , item.Path // CalDAV / CardDAV Principal URL. Here we can return ((item as ICurrentUserPrincipal).GetCurrentUserPrincipal()).Path if needed.
            , (context as DavContext).UserName // user name
            , url.Port // port                
            , (url.Scheme == "https").ToString().ToLower() // SSL
            , decription // CardDAV / CardDAV Account Description
            , Assembly.GetAssembly(this.GetType()).GetName().Version.ToString()
            , Assembly.GetAssembly(typeof(DavEngine)).GetName().Version.ToString()
            , payloadUUID

        byte[] profileBytes = SignProfile(context, profile);

        context.Response.ContentType = "application/x-apple-aspen-config";
        context.Response.AddHeader("Content-Disposition", "attachment; filename=profile.mobileconfig");
        context.Response.ContentLength = profileBytes.Length;
        using (BinaryWriter writer = new BinaryWriter(context.Response.OutputStream))

    private byte[] SignProfile(DavContextBase context, string profile)
        // Here you will sign your profile with SSL certificate to avoid "Unsigned" warning on iOS and OS X.
        // For demo purposes we just return the profile content unmodified.
        return context.Engine.ContentEncoding.GetBytes(profile);
See Also