Developer forum

Forum » Dynamicweb 10 » Api controllers

Api controllers

Karol Barkowski
Reply

Hi,

in DW9 it was possible to use this little piece of code that allowed to add custom api controllers to the exisdting DW project:

    [Subscribe(Standard.Application.AuthenticateRequest)]
    public class ApiEnabler : NotificationSubscriber
    {
        public const string RoutePrefix = "api/custom";
        public override void OnNotify(string notification, NotificationArgs args)
        {
            if (args is not Standard.Application.AuthenticateRequestArgs authenticateArgs) return;
            var requestPath = HttpContext.Current.Request.Path;
            var isApiRequest = requestPath.StartsWith($"/{RoutePrefix}");

            if (isApiRequest)
            {
                HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
                authenticateArgs.Handled = true;
                return;
            }
        }
    }

 

Apparently, that doesn't work anymore in DW10 and this notification subscriber is not even triggered anymore.

What is the approach to add an api controller to DW10 project now? 

regards

 

 


Replies

 
Karol Barkowski
Reply

Or maybe same question but asked in a different way - how to tell DW to not intercept a route to my controller and redirect to 404 page?

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Karol,

You can create a pipeline in which you can register controllers, middleware or services. All you need to do is create a class that implements the Dynamicweb.Host.Core.IPipeline interface. This gives you access to the service collection, the application builder and the mvc builder. There are two route prefixes reserved by DynamicWeb, /admin and /dwapi, all other routes can be mapped by you. The last pipeline to execute is the DynamicWeb Frontend, so it will only handle routes that are not explicitly mapped.

This GitHub project has an example where we register a GRPC service: https://github.com/dynamicweb/Summit2022_APILandscape

I hope this helps.

- Jeppe

 
Karol Barkowski
Reply

Hi,

that repo you linked not only doesn't show how to use that IPipeline interface but it literally has nothing to do with DynamicWeb itself.
I looked at the docs and there's also nothing about IPipeline interface and how it could be used.
Are there any live examples where it is actually shown how to use that interface in DW project?

 

 

 

You must be logged in to post in the forum