c# - Doing some actions (setting language) before controller initializes in Web API -
i trying set language
controller
in web api
(doing internationalization globalization).
i writing attribute called setlanguage
, decorate web-api controller
it
something like,
[setlanguage] public servicerequestcontroller : apicontroller
but problem this:
public class setacceptlanguageheader : attribute, icontrollerconfiguration { public void initialize(httpcontrollersettings controllersettings, httpcontrollerdescriptor controllerdescriptor) { if (controllersettings.request.headers.acceptlanguage != null && controllercontext.request.headers.acceptlanguage.count > 0) { var culture = new cultureinfo(controllercontext.request.headers.acceptlanguage.first().value); thread.currentthread.currentculture = culture; thread.currentthread.currentuiculture = culture; } } }
but, not able because cannot access request
controllersettings
.
(though know idea of using basecontroller
, overriding initialize
method achieve same, trying use [setlanguage]
attribute`)
any ideas how ?
you may try using action filters like
public class setlanguageattribute : actionfilterattribute { public override void onactionexecuting(httpactioncontext actioncontext) { //use actioncontext.request access request } }
this can used decorate action method [setlanguage] attribute can possibly set culture needed.
Comments
Post a Comment