我的Enum属性会更改为http-response吗?

时间:2018-10-01 16:45:22

标签: c# json api asp.net-core

我有一个web-service并有以下代码:

roomStay = new RoomStay
            {
                PlaceType = stay.hotelInfo.propertyType,
                GeneralDescription = GenerateGeneralDescription(stay.hotelInfo).Result,
                IsRefundableRoom = !stay.nonRefundable,
                ProviderType = Supplier.BetterBooking,
                BasicPropertyInfo = new BasicPropertyInfo
                {
                    SupplierCode = stay.hotelInfo?.hotelName + stay.hotelInfo?.areaName
                                                          ,
                    Images = imageList,
                    NewImages = newImageList.ToArray()
                                                          ,
                    Address = $"{area + address + city + countryName  }" //$"{hotelStaticDataResult.hotelInfo?.address?.countryName} {hotelStaticDataResult.hotelInfo?.address?.cityName} {string.Join(" ", hotelStaticDataResult.hotelInfo?.address?.addressLines.ToArray())}"
                                                          ,
                    City = ""//cityName
                                                          ,
                    Name = stay.hotelInfo.hotelName
                                                          ,
                    Facilities = new List<FacilityCategory>()//facilitieList
                                                            ,
                    Longitude = longitude//hotelStaticDataResult.hotelInfo?.position.longitude
                                                            ,
                    Latitude = latitude// hotelStaticDataResult.hotelInfo?.position.latitude
                                                            ,
                    Telephone = string.Empty//hotelStaticDataResult?.hotelInfo?.contactNumbers?.Where(a => a.phoneType == "Phone").FirstOrDefault()?.phoneNumber
                                                            ,
                    Description = string.Empty,
                    TextItems = stay.hotelInfo?.textItems.Select(a => new TextItemsModelOTA()
                    {
                        Title = a.title == null || string.IsNullOrEmpty(a.title) ? string.Empty : a.title,
                        Description = a.description == null || string.IsNullOrEmpty(a.description) ? string.Empty : a.description,
                        TitleEnumType = (TitleEnumType)Utility.GetEnum<TitleEnumType>(a.title).Result
                    })//hotelStaticDataResult.hotelInfo?.textItems.ToJson()
                                                            ,
                    Code = hotelKey// searchrs.Result.transactionId + "$" + searchrs.Result.searchId + "$" + stay.stayId + "$" + stay.hotelInfo.hotelName
                                                            ,
                    RateProviderKeyValues = new List<RateProviderKeyValue>() { new RateProviderKeyValue() { Key = decimal.Parse(stay.hotelInfo.starRating.ToStringSafe()), Value = RateProvider.AAA } }.ToArray()

                },
                RoomRates = roomRate
                                                ,
            };

TextItem属性中,我具有TextEnumType属性,该类型为枚举类型:

public enum TitleEnumType
    {
        Facilities = 0,
        Rooms = 1,
        Meals = 2,
        Payment = 3,
        PropertyDescription = 4,
        NumberOfRooms = 5,
        CheckInTime = 6,
        CheckOutTime = 7,
        HotelRemarks = 8,
        None = 9
    }

使用以下的拖曳方法,我使用GetStringForEnum(string model)方法获取字符串,并使用Enum转换为GetEnum<T>(string model),例如:

public static int GetEnum<T>(string model)
    {
        var newModel = GetStringForEnum(model);

        if (!Enum.IsDefined(typeof(T), newModel))
        {
            return (int)Enum.Parse(typeof(T), "None", true);

        }

        return (int)Enum.Parse(typeof(T), newModel, true);
    }

    private static string GetStringForEnum(string model)
    {
        Regex rgx = new Regex("[^a-zA-Z0-9 -]");
        var nonAlphanumericData = rgx.Matches(model);
        if (nonAlphanumericData.Count < 0)
        {
            return model;
        }
        foreach (var item in nonAlphanumericData)
        {
            model = model.Replace((string)item, "");
        }
        return model;
    }

注意:GetEnum<T>(string model)方法将枚举值设为true。

在生成roomStay模型之后,在发送此roomStay http-response之前,我所有的数据都是正确的。但是当我在自己的网站和roomStay中收到此controller时,此roomStay是不正确的。

我怎么了?

1 个答案:

答案 0 :(得分:1)

这是因为您为获取枚举而调用的方法是异步的。因此,当您返回RoomStay对象时,TextItems.TitleEnumType的默认值为0。尝试:

public static GetEnum<T>(string model)
    {
            var newModel = GetStringForEnum(model);

            if (!Enum.IsDefined(typeof(T), newModel.Result))
            {
                var test1 = (int)Enum.Parse(typeof(T), "None", true);
                return test1;
            }

            var test = (int)Enum.Parse(typeof(T), newModel.Result, true);
            return test;
    }

    private static GetStringForEnum(string model)
    {
            Regex rgx = new Regex("[^a-zA-Z0-9 -]");
            var nonAlphanumericData = rgx.Matches(model);
            if (nonAlphanumericData.Count < 0)
            {
                return model;
            }
            foreach (var item in nonAlphanumericData)
            {
                model = model.Replace((string)item, "");
            }
            return model;
    }