I am working on CMS which contains many domains (so our URLs can look like www.example.com/group or www.example.com/de). I want to make a call from frontend based on current domain and then I need to be able to send it to a endpoint in the backend, which needs to have a dynamic routing based on domain it currently receives request from. This is my controller:
[Route("CookieHandler")]
public class CookieHandlerController : Controller
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ICookieService _cookieService;
public CookieHandlerController(IHttpContextAccessor httpContextAccessor, ICookieService cookieService)
{
_httpContextAccessor = httpContextAccessor;
_cookieService = cookieService;
}
[HttpGet]
[Route("UpdateCookies")]
public IActionResult UpdateCookies()
{
var requestCookies = _httpContextAccessor.HttpContext?.Request.Cookies;
foreach (var cookie in requestCookies)
{
_cookieService.UpdateCookiesAccordingToConsent(cookie.Key);
}
return Ok();
}
}
So, basically the endpoint routing needs to look like this:
www.example.com/group/updatecookies
or
www.example.com/fr/updatecookies
The reason for such strange setup is because there are some Http-only cookies which have their specific path value based on their domain. One more thing to add is that there are so many domains that simply hardcoding them might not work.
What I have tried:
I have tried to achieve it with that setup:
[Route("{domain}")]
public class CookieHandlerController : Controller
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ICookieService _cookieService;
private readonly string _domain;
public CookieHandlerController(IHttpContextAccessor httpContextAccessor, ICookieService cookieService, string domain)
{
_httpContextAccessor = httpContextAccessor;
_cookieService = cookieService;
_domain = domain;
}
[HttpGet]
[Route("updatecookies")]
public IActionResult UpdateCookies()
{
var test = _httpContextAccessor.HttpContext.Request.Path;
var requestCookies = _httpContextAccessor.HttpContext?.Request.Cookies;
foreach (var cookie in requestCookies)
{
_cookieService.UpdateCookiesAccordingToConsent(cookie.Key);
}
return Ok();
}
}
It doesn't work and I am getting a 500 error. Is what I am trying to achieve possible? Maybe it needs some specific controllermapping in the Startup class?