WebApi控制器失败 - 线程文化问题

时间:2018-03-21 13:52:38

标签: c# asp.net-core-2.0 web.api

我有一个在return语句

上失败的控制器方法
[HttpGet("{id}")]
public UserModel Get(int id)
{
    System.Threading.Thread.CurrentThread.CurrentCulture =
        new System.Globalization.CultureInfo("en-CA");

    System.Threading.Thread.CurrentThread.CurrentUICulture =
        new System.Globalization.CultureInfo("en-CA");

    BP.DataAccess.DataUtilities.DatabaseCommand.ConnectionString =
        _configuration["connectionStrings:PrimaryConnection"];

    UserModel user = UserService.GetUserById(id);

    return user;
}

UserModel班有:

public int RoleTypeId { get; set; }
private ListModels.RoleTypeModel _RoleType;
public ListModels.RoleTypeModel RoleType
{
    get
    {
        _RoleType = Trisura.BP.Core.ListServices
            .RoleTypeService.GetRoleTypeById(RoleTypeId);
        return _RoleType;
    }
    private set { _RoleType = value; }
}

public static RoleTypeModel GetRoleTypeById(int id)
{
    int cultureId = CultureService.GetThreadCultureId();

    List<ListModels.RoleTypeModel> localizedList =
        GetRoleTypesLocalizedList(cultureId);

    ListModels.RoleTypeModel roleTypeModel =
        localizedList.Where(roletype => roletype.Id == id).FirstOrDefault();

    if (roleTypeModel == null)
    {
        throw new CoreExceptions.ObjectNotFoundInDatabaseException(
            string.Format("The requested role type (ID = {0}) was not found.", id));
    }

    return roleTypeModel;
}

public static RoleTypeModel GetRoleTypeById(int id)
{
    // this returns "en-US" (0)  instead of "en-CA" (1)
    int cultureId = CultureService.GetThreadCultureId(); 

    List<ListModels.RoleTypeModel> localizedList = GetRoleTypesLocalizedList(cultureId);
    ListModels.RoleTypeModel roleTypeModel =
        localizedList.Where(roletype => roletype.Id == id).FirstOrDefault();

    if (roleTypeModel == null)
    {
        throw new CoreExceptions.ObjectNotFoundInDatabaseException(
            string.Format("The requested role type (ID = {0}) was not found.", id));

        return roleTypeModel;
    }
}

当控制器点击return user;时,它会失败,因为线索CurrentCulture设置为en-US,即使我明确说文化是en-CA

有没有人有解释? 这是否发生在一个单独的线程中? 如何确保我的模型能够以相同的文化运行?

0 个答案:

没有答案