我是Web API的新手,我正在尝试将其集成到我正在处理的项目中。
我正在向WebApi操作发布一个复杂类型,但是当我这样做时,它会调用我的类型构造函数,最终会覆盖我发布到方法的值。
这是正常的吗?我在fiddler中看到了实际值,我希望在反序列化对象时保留它们。我应该以不同的方式实现这一目标吗?任何帮助或知识将不胜感激!谢谢! :)
这是我的班级:
public class PageOptions
{
public List<ControlOption<MacroDataType>> DataTypeOptions { get; set; }
public List<ControlOption<string>> FcastSourceOptions { get; set; }
public ModalOptions<CorpRegionFilter, CorpRegionSort> CorpCodeOptions { get; set; }
public ModalOptions<EmptyFilter, EmptySort> BusinessLineOptions { get; set; }
public ModalOptions<EmptyFilter, EmptySort> SubCategoryOptions { get; set; }
public List<ControlOption<string>> RollingMonthOptions { get; set; }
public List<ModalSelection<BusLineInfo>> AddBusinessLineOptions { get; set; }
public List<ControlOption<AxisDates>> AxisDateOptions { get; set; }
public List<ControlOption<GraphOptions>> GraphOptionsOptions { get; set; }
public List<MacroLineOptions> MacroLines { get; set; }
public List<MacroNote> MacroNotes { get; set; }
public List<CompareLineOptions> CompareLines { get; set; }
public Boolean CompareAllToOne { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public GraphStyles GraphStyle { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public CalendarStyles CalendarStyle { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public AxisDates AxisDate { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public LineCompareType ComparisonType { get; set; }
public String Title { get; set; }
public String SubTitle { get; set; }
public Boolean ScaleResults { get; set; }
public Boolean ShowTabulation { get; set; }
public Boolean ThickLines { get; set; }
public Boolean BrowseByCorp { get; set; }
public Boolean ShowUserName { get; set; }
public Boolean PrintLargeFont { get; set; }
public Boolean Enable3D { get; set; }
public Boolean Launch { get; set; }
public Boolean Promotion { get; set; }
public Boolean PriceEIChange { get; set; }
public Boolean LocalEvent { get; set; }
public Boolean Miscellaneous { get; set; }
public BrowseByCorpData BrowseByCorpData { get; set; }
public List<List<string>> LineColors { get; set; }
public String NativeId { get; set; }
public String FullUserName { get; set; }
public MacroLineOptions DefaultLineConfig { get; set; }
public List<List<string>> DefaultLineColors { get; set; }
public LineFormStates FormStates { get; set; }
public int CurrentLineIndex { get; set; }
public String ChartImageURI { get; set; }
public PageOptions()
{
TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
// Current User Id
NativeId = HttpContext.Current.User.Identity.Name;
using (var client = new MacroDataServiceClient())
{
FullUserName = client.GetUsersFullName(NativeId);
}
// Add the Lines
MacroLines = new List<MacroLineOptions>();
MacroLines.Add(new MacroLineOptions() { LineId = 1, Description = "Line 1" });
MacroLines.Add(new MacroLineOptions() { LineId = 2, Description = "Line 2" });
MacroLines.Add(new MacroLineOptions() { LineId = 3, Description = "Line 3" });
MacroLines.Add(new MacroLineOptions() { LineId = 4, Description = "Line 4" });
MacroLines.Add(new MacroLineOptions() { LineId = 5, Description = "Line 5" });
MacroLines.Add(new MacroLineOptions() { LineId = 6, Description = "Line 6" });
MacroLines.Add(new MacroLineOptions() { LineId = 7, Description = "Line 7" });
MacroLines.Add(new MacroLineOptions() { LineId = 8, Description = "Line 8" });
MacroLines.Add(new MacroLineOptions() { LineId = 9, Description = "Line 9" });
MacroLines.Add(new MacroLineOptions() { LineId = 10, Description = "Line 10" });
DefaultLineConfig = new MacroLineOptions() { LineId = 1, Description = "" };
// Add Business Line Options
AddBusinessLineOptions = new List<ModalSelection<BusLineInfo>>();
using (var client = new MacroDataServiceClient())
{
client.GetAllBusinessLineData()
.Where(x => x.BusLnCd == "A64" || x.BusLnCd == "A08" || x.BusLnCd == "A54" || x.BusLnCd == "A85" || x.BusLnCd == "A09")
.ToList()
.ForEach(x =>
{
AddBusinessLineOptions.Add(new ModalSelection<BusLineInfo>() { Description = textInfo.ToTitleCase(x.BusLnNm.ToLower()), Selections = new List<BusLineInfo>() { new BusLineInfo() { Code = x.BusLnCd, Name = textInfo.ToTitleCase(x.BusLnNm.ToLower()) } } });
});
}
// DATATYPES
DataTypeOptions = new List<ControlOption<MacroDataType>>();
using (((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate())
using (var client = new MacroDataServiceClient())
{
client.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
var sales = client.GetFilteredMacroDataTypes(MacroDataTypeSection.Sales).Select(x => new ControlOption<MacroDataType>(x.DataType, x.DisplayName, "Sales")).ToList();
var recruiting = client.GetFilteredMacroDataTypes(MacroDataTypeSection.Recruiting).Select(x => new ControlOption<MacroDataType>(x.DataType, x.DisplayName, "Distributor Force")).ToList();
var other = client.GetFilteredMacroDataTypes(MacroDataTypeSection.Other).Select(x => new ControlOption<MacroDataType>(x.DataType, x.DisplayName, "Other")).ToList();
var daily = client.GetFilteredMacroDataTypes(MacroDataTypeSection.Daily).Select(x => new ControlOption<MacroDataType>(x.DataType, x.DisplayName, "Daily")).ToList();
DataTypeOptions.Add(new ControlOption<MacroDataType>(MacroDataType.NoSelection, "No Selection"));
DataTypeOptions.AddRange(sales);
DataTypeOptions.AddRange(recruiting);
DataTypeOptions.AddRange(other);
DataTypeOptions.AddRange(daily);
}
// FCAST SOURCE
FcastSourceOptions = new List<ControlOption<string>>();
FcastSourceOptions.Add(new ControlOption<string>("00", "Current Fcst"));
int count = 1;
for (var date = DateTime.Now.AddMonths(-1); date >= DateTime.Now.AddMonths(-61); date = date.AddMonths(-1))
{
var strVal = String.Format("{0:00}", count++);
var strText = String.Format("{0:00}/{1} EOM Fcst", date.Month, date.Year);
FcastSourceOptions.Add(new ControlOption<string>(strVal, strText));
}
// CORP CODES
CorpCodeOptions = new ModalOptions<CorpRegionFilter, CorpRegionSort>();
CorpCodeOptions.SortList.Add(new ModalDropdownOption { Selected = false, Text = "Corp Code", Value = (int)CorpRegionSort.CorpCode });
CorpCodeOptions.SortList.Add(new ModalDropdownOption { Selected = false, Text = "Corp Name", Value = (int)CorpRegionSort.CorpName });
CorpCodeOptions.SortList.Add(new ModalDropdownOption { Selected = false, Text = "Region/Corp Code", Value = (int)CorpRegionSort.RegionCorpCode });
CorpCodeOptions.SortList.Add(new ModalDropdownOption { Selected = false, Text = "Region/Corp Name", Value = (int)CorpRegionSort.RegionCorpName });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "status", Selected = false, Text = "All", Value = (int)CorpRegionFilter.AllStatus, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "status", Selected = true, Text = "Active", Value = (int)CorpRegionFilter.ActiveStatus, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "status", Selected = false, Text = "Planned", Value = (int)CorpRegionFilter.PlannedStatus, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "region", Selected = true, Text = "All", Value = (int)CorpRegionFilter.AllRegion, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "region", Selected = false, Text = "AAP", Value = (int)CorpRegionFilter.AAPRegion, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "region", Selected = false, Text = "AJL", Value = (int)CorpRegionFilter.AJLRegion, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "region", Selected = false, Text = "EU", Value = (int)CorpRegionFilter.EURegion, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "region", Selected = false, Text = "NABR", Value = (int)CorpRegionFilter.NABRRegion, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Group = "region", Selected = false, Text = "US", Value = (int)CorpRegionFilter.USRegion, OptionType = "radio" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Selected = false, Text = "Catalog", Value = (int)CorpRegionFilter.Catalog, OptionType = "checkbox" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { Selected = false, Text = "Other", Value = (int)CorpRegionFilter.Other, OptionType = "checkbox" });
CorpCodeOptions.FilterList.Add(new ModalDropdownOption { });
CorpCodeOptions.SelectedSort = CorpRegionSort.RegionCorpCode;
// BUSINESS LINES
BusinessLineOptions = new ModalOptions<EmptyFilter, EmptySort>();
// Add filters/Sorts here
// SUBCAT
SubCategoryOptions = new ModalOptions<EmptyFilter, EmptySort>();
// Add filters/Sorts here
// ROLLING MONTHS
RollingMonthOptions = new List<ControlOption<string>>();
RollingMonthOptions.Add(new ControlOption<string>("1", "1 Month"));
RollingMonthOptions.Add(new ControlOption<string>("2", "2 Month"));
RollingMonthOptions.Add(new ControlOption<string>("3", "3 Month"));
RollingMonthOptions.Add(new ControlOption<string>("4", "4 Month"));
RollingMonthOptions.Add(new ControlOption<string>("5", "6 Month"));
RollingMonthOptions.Add(new ControlOption<string>("7", "7 Month"));
RollingMonthOptions.Add(new ControlOption<string>("8", "8 Month"));
RollingMonthOptions.Add(new ControlOption<string>("9", "9 Month"));
RollingMonthOptions.Add(new ControlOption<string>("10", "10 Month"));
RollingMonthOptions.Add(new ControlOption<string>("11", "11 Month"));
RollingMonthOptions.Add(new ControlOption<string>("12", "12 Month"));
RollingMonthOptions.Add(new ControlOption<string>("18", "18 Month"));
RollingMonthOptions.Add(new ControlOption<string>("24", "24 Month"));
RollingMonthOptions.Add(new ControlOption<string>("36", "36 Month"));
RollingMonthOptions.Add(new ControlOption<string>("48", "48 Month"));
RollingMonthOptions.Add(new ControlOption<string>("60", "60 Month"));
RollingMonthOptions.Add(new ControlOption<string>("72", "72 Month"));
RollingMonthOptions.Add(new ControlOption<string>("84", "84 Month"));
RollingMonthOptions.Add(new ControlOption<string>("96", "96 Month"));
RollingMonthOptions.Add(new ControlOption<string>("108", "108 Month"));
RollingMonthOptions.Add(new ControlOption<string>("120", "120 Month"));
RollingMonthOptions.Add(new ControlOption<string>("132", "132 Month"));
RollingMonthOptions.Add(new ControlOption<string>("144", "144 Month"));
RollingMonthOptions.Add(new ControlOption<string>("156", "156 Month"));
RollingMonthOptions.Add(new ControlOption<string>("168", "168 Month"));
RollingMonthOptions.Add(new ControlOption<string>("180", "180 Month"));
RollingMonthOptions.Add(new ControlOption<string>("192", "192 Month"));
RollingMonthOptions.Add(new ControlOption<string>("204", "204 Month"));
RollingMonthOptions.Add(new ControlOption<string>("216", "216 Month"));
RollingMonthOptions.Add(new ControlOption<string>("228", "228 Month"));
RollingMonthOptions.Add(new ControlOption<string>("240", "240 Month"));
// MACRO NOTES
MacroNotes = new List<MacroNote>();
GraphOptionsOptions = new List<ControlOption<GraphOptions>>();
GraphOptionsOptions.Add(new ControlOption<GraphOptions>(GraphOptions.ShowTabulation, "Show Tabulation", null, GraphOptions.ShowTabulation.ToString()));
GraphOptionsOptions.Add(new ControlOption<GraphOptions>(GraphOptions.ThickLines, "Thick Lines", null, GraphOptions.ThickLines.ToString()));
GraphOptionsOptions.Add(new ControlOption<GraphOptions>(GraphOptions.BrowseByCorp, "Browse By Corp", null, GraphOptions.BrowseByCorp.ToString()));
GraphOptionsOptions.Add(new ControlOption<GraphOptions>(GraphOptions.ShowUserName, "Show User Name", null, GraphOptions.ShowUserName.ToString()));
//GraphOptionsOptions.Add(new ControlOption<GraphOptions>(GraphOptions.ShowNotes, "Show Notes", null, GraphOptions.ShowNotes.ToString()));
//GraphOptionsOptions.Add(new ControlOption<GraphOptions>(GraphOptions.PrintLargeFont, "Print Large Font", null, GraphOptions.PrintLargeFont.ToString()));
//GraphOptionsOptions.Add(new ControlOption<GraphOptions>(GraphOptions.Enable3D, "Enable 3D", null, GraphOptions.Enable3D.ToString()));
GraphOptionsOptions.Add(new ControlOption<GraphOptions>(GraphOptions.ScaleResults, "Scale Results", null, GraphOptions.ScaleResults.ToString()));
// Add the maximum number of comparison lines
CompareLines = new List<CompareLineOptions>();
CompareLines.Add(new CompareLineOptions());
CompareLines.Add(new CompareLineOptions());
CompareLines.Add(new CompareLineOptions());
CompareLines.Add(new CompareLineOptions());
CompareLines.Add(new CompareLineOptions());
LineColors = new List<List<String>>() { new List<String>() { "#3366cc", "#dc3912", "#ff9900", "#109618" }, new List<String>() { "#990099", "#0099c6", "#dd4477", "#66aa00" }, new List<String>() { "#b82e2e", "#316395" } };
DefaultLineColors = LineColors;
// Add other options
FormStates = new LineFormStates();
GraphStyle = GraphStyles.Trend;
CalendarStyle = CalendarStyles.Calendar;
ComparisonType = LineCompareType.None;
ShowTabulation = true;
ThickLines = true;
ShowUserName = true;
PrintLargeFont = true;
Enable3D = true;
ScaleResults = true;
CurrentLineIndex = 0;
BrowseByCorpData = new BrowseByCorpData() { BrowseByCorpList = new List<CorpInfo>(), SelectedCorp = new CorpInfo() };
}
}
这是我的Post方法:
[HttpPost]
[ActionName("SimplePost")]
public void PostPageOptions([FromBody] PageOptions value)
{
}
就像我提到的那样,我的小提琴手显示了正确的值,但似乎它调用了我的构造函数,并使用我的初始化值而不是Web API方法。
我的班级太大了吗?当我使用较小的类时,它似乎有效。
答案 0 :(得分:0)
是的,这是正常的。当您将复杂类型传递给web api时,对象实际上由api控制器作为字符串读取并序列化为由签名确定的正确对象。但要执行此操作,将调用类的基本构造函数,因此在您的情况下,将调用无参数构造函数,然后控制器尝试填充在对象的sting表示中传递的属性。基本上要修复它你需要有一个没有初始化值的基本无参数构造函数