Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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:

C#
[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:
C#
	[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?
Posted
Updated 28-Apr-23 0:35am

1 solution

Since you hit 500 error which is server-side error it might suggest that you have some deeper issue but to be able to handle /*/updatecookies pattern you need
1. Remove route from controller
[Route("/")]

2. Setup following route for your handler method
[HttpGet("{domain}/updateCookies")]
public int UpdateCookies(string domain)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900