我想用不同的語言創建網站。我已經讀過,我可以創造一個ActionFilter,但我有一個豆蔻問題:
我不得不爲了創建一個自定義模型綁定器與英語和德語數字格式(123,456,789.1
對123.456.789,1
)在使用ModelBinder之前更改文化
public class DecimalModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
string key = bindingContext.ModelName;
var v = ((string[])bindingContext.ValueProvider.GetValue(key).RawValue)[0];
float outPut;
if (float.TryParse(v, NumberStyles.Number, System.Globalization.CultureInfo.CurrentCulture, out outPut))
return outPut;
return base.BindModel(controllerContext, bindingContext);
}
}
這個工作ModelBinder使用當前的文化來決定使用哪種格式。 但是不幸的是,ModelBinder在ActionFilter可以改變文化之前使用。
如何在ModelBinder變爲活動狀態之前更改的文化?
它的工作,謝謝了很多!但我有一個問題:你知道,爲什麼代碼每次請求執行5次? – Christopher
我沒有得到那種行爲。你可以添加一個手錶((System.Web.HttpApplication)(sender))。Request.RawUrl並查看請求是否相同? – barry
@Christopher該代碼每個請求執行一次(因此名稱爲BeginRequest),因此可能有一個頁面請求,還有4個樣式表,圖像或JavaScript文件。檢查Request.Url,這可能會有所幫助。 –