Can someone please add a code example showing how to setup a custom webapi in a DW10 solution.
Can't use the DW9 way with WebApiEnabler anymore I guess.
Developer forum
E-mail notifications
Custom webapi in DW10
Replies
+1 :-)
@Martin Moen i just got it working on a DW 10.3.4 here.
I've got a controller I routed to /api/[controller]/[action]" in DW9. I changed the controller a bit in DW10. It looks something like this:
namespace YourNamespace
[Route("/api/[controller]/[action]")]
public class YourController : ControllerBase
{
[HttpGet]
public Async Task<IActionResult> YourGetMethod(string param1, int param2)
{
//Code here...
}
}
To enable the route for "/api" all I had to do was create a Pipeline like this (don't know for sure if this is the correct way to do it, but it's working ;-)):
using Dynamicweb.Host.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace Application.Pipelines
{
public sealed class YourPipeline : IPipeline
{
public int Rank { get; }
public void RegisterApplicationComponents(IApplicationBuilder app) {
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(options =>
{
options.MapControllerRoute("api", "api/{controller}/{action}");
});
}
public void RegisterServices(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
{
}
public void RunInitializers()
{
}
}
}
Nice, thanks for sharing Rene!
I will test it out :)
Just remember, that if you install your extention via local apps (appstore), then you need the application to restart before the pipeline is picked up.
/Kenneth
Hi all,
This is indeed the recommended way to do this. We even have a sample project from Summit 2022 showing exactly this scenario -- though in that case it was a GRPC endpoint that we register, but the principles are the same. https://github.com/dynamicweb/Summit2022_APILandscape
Also, if you have source code access to DynamicWeb 10, you can see how we do it. The way we register routes and endpoints is the same way we recommend you do it. You can look at any of the 8 pipelines that come with DW10 Suite.
@Rene: Be aware that in your example, your setup is done globally and not just for the route(s) that you register. You can use the app.MapWhen method to register your configuration for /api/* endpoints only. Otherwise you risk activating controllers or middleware for routes that shouldn't have them.
- Jeppe
You must be logged in to post in the forum