{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Mvc/issues/5589","repository_url":"https://api.github.com/repos/aspnet/Mvc","labels_url":"https://api.github.com/repos/aspnet/Mvc/issues/5589/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Mvc/issues/5589/comments","events_url":"https://api.github.com/repos/aspnet/Mvc/issues/5589/events","html_url":"https://github.com/aspnet/Mvc/issues/5589","id":192813327,"number":5589,"title":"Accessing the request's cookie in Controller's constructor","user":{"login":"Saticmotion","id":5888357,"avatar_url":"https://avatars.githubusercontent.com/u/5888357?v=3","gravatar_id":"","url":"https://api.github.com/users/Saticmotion","html_url":"https://github.com/Saticmotion","followers_url":"https://api.github.com/users/Saticmotion/followers","following_url":"https://api.github.com/users/Saticmotion/following{/other_user}","gists_url":"https://api.github.com/users/Saticmotion/gists{/gist_id}","starred_url":"https://api.github.com/users/Saticmotion/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Saticmotion/subscriptions","organizations_url":"https://api.github.com/users/Saticmotion/orgs","repos_url":"https://api.github.com/users/Saticmotion/repos","events_url":"https://api.github.com/users/Saticmotion/events{/privacy}","received_events_url":"https://api.github.com/users/Saticmotion/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":8,"created_at":"2016-12-01T10:57:18Z","updated_at":"2016-12-02T14:35:33Z","closed_at":"2016-12-02T13:47:15Z","body":"Since I'm not happy with the default Localization system in ASP.NET Core, I've written my own. (Main problem is that you have to create separate files for all views and controllers)\r\n\r\nIt can select the language based on Cookies. To cut down on typing I wanted to be able to get the Cookie in the Controller's constructor, but Request, Response and HttpContext are all null in the constructor of the Controller. I've read that past versions of ASP.NET had methods that were called before executing Actions, but I can't seem to find these in ASP.NET Core.\r\n\r\nMy code works, but I have to get the cookie and pass it to a new localizer instance in each request.\r\n\r\nI've often seen middleware mentioned to solve this kind of problem, but I'm somewhat certain it won't work for my case. Reason being that I want to set the localizer instance of the controller to the one I configure with the cookie. This does not seem to be possible, since the middleware doesn't know that my controllers have a localizer instance (I could try to figure out the controller type and cast it in the middleware, but that's just silly)\r\n\r\nIf there's a way to do what I want with middleware, I'd love to hear it. If not, is there any way to execute code before requests to a certain controller? Constructor seems to be too early in the request cycle, since Request, Response and HttpContext are all null in the constructor.\r\n\r\nThanks in advance!\r\nSimon"},"comment":{"url":"https://api.github.com/repos/aspnet/Mvc/issues/comments/264467354","html_url":"https://github.com/aspnet/Mvc/issues/5589#issuecomment-264467354","issue_url":"https://api.github.com/repos/aspnet/Mvc/issues/5589","id":264467354,"user":{"login":"ShadowDancer","id":766312,"avatar_url":"https://avatars.githubusercontent.com/u/766312?v=3","gravatar_id":"","url":"https://api.github.com/users/ShadowDancer","html_url":"https://github.com/ShadowDancer","followers_url":"https://api.github.com/users/ShadowDancer/followers","following_url":"https://api.github.com/users/ShadowDancer/following{/other_user}","gists_url":"https://api.github.com/users/ShadowDancer/gists{/gist_id}","starred_url":"https://api.github.com/users/ShadowDancer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ShadowDancer/subscriptions","organizations_url":"https://api.github.com/users/ShadowDancer/orgs","repos_url":"https://api.github.com/users/ShadowDancer/repos","events_url":"https://api.github.com/users/ShadowDancer/events{/privacy}","received_events_url":"https://api.github.com/users/ShadowDancer/received_events","type":"User","site_admin":false},"created_at":"2016-12-02T14:35:33Z","updated_at":"2016-12-02T14:35:33Z","body":"You may cache it in private field, just using lazy prevents user from accessing backing field."}}
{"actor":{"display_login":"ShadowDancer"}}