C# 控制器中的模式匹配以路由请求
假设我有一个控制器:C# 控制器中的模式匹配以路由请求,c#,.net,visual-studio-2017,asp.net-web-api2,c#-7.0,C#,.net,Visual Studio 2017,Asp.net Web Api2,C# 7.0,假设我有一个控制器: public class MyController : ApiController { [Route("Order")] public List<MyResponse> Post([FromBody] Order request) { //validation return stuff; } } 假设我们的订单对象是一个令人恶心的烂摊子: public class Order {
public class MyController : ApiController
{
[Route("Order")]
public List<MyResponse> Post([FromBody] Order request)
{
//validation
return stuff;
}
}
假设我们的订单对象是一个令人恶心的烂摊子:
public class Order
{
public int noOfWidgets {get;set;}
public bool IsSelected { get; set; }
public bool? DisperseCharges { get; set; }
public bool? IsAdjustable { get; set; }
public bool? IsPackageChild { get; set; }
public byte ChargeTypeID { get; set; }
public byte PriceClass { get; set; }
public DateTime BeginDate { get; set; }
public DateTime ChargeBeginDate { get; set; }
public DateTime ChargeEndDate { get; set; }
public DateTime PeriodBeginDate { get; set; }
public DateTime PeriodEndDate { get; set; }
public DateTime QuoteDate { get; set; }
public DateTime? EndDate { get; set; }
public DateTime? PurchaseDate { get; set; }
public DateTime? StepBeginDate { get; set; }
public DateTime? StepEndDate { get; set; }
public decimal? AgentCost { get; set; }
public decimal? Amount { get; set; }
public decimal? BeginRecurs { get; set; }
public decimal? BilledAmount { get; set; }
public decimal? BookedCost { get; set; }
public decimal? ChargeBeginRecurs { get; set; }
public decimal? ChargeEndRecurs { get; set; }
public decimal? ChargeQuantity { get; set; }
public decimal? ChargeRecurs { get; set; }
public decimal? DisperseAmount { get; set; }
public decimal? EndRecurs { get; set; }
public decimal? ETLCost { get; set; }
public decimal? Factor { get; set; }
public decimal? Multiplier { get; set; }
public decimal? NonPayCharge { get; set; }
public decimal? NonPayRate { get; set; }
public decimal? NumberOfPeriodsBilled { get; set; }
public decimal? OtherCost { get; set; }
public decimal? RealCost { get; set; }
public decimal? ScheduleBeginRecurs { get; set; }
public decimal? ScheduleEndRecurs { get; set; }
public decimal? ScheduleTotalRecurs { get; set; }
public decimal? SPIFFCost { get; set; }
public decimal? StandardCharge { get; set; }
public decimal? StandardRate { get; set; }
public decimal? SuspendCharge { get; set; }
public decimal? SuspendRate { get; set; }
public Guid RequestUID { get; set; }
public int ChargeClassID { get; set; }
public int CycleScheduleID { get; set; }
public int ElementClassID { get; set; }
public int ID { get; set; }
public int ProductComponentID { get; set; }
public int Quantity { get; set; }
public int QuoteID { get; set; }
public int YYYYMMCCID { get; set; }
public int? AccountID { get; set; }
public int? ARItemID { get; set; }
public int? BillingChargeClassID { get; set; }
public int? BillingFrequencyMask { get; set; }
public int? BillingNumber { get; set; }
public int? ChargeDays { get; set; }
public int? ChargeIsWaivable { get; set; }
public int? ComponentClassID { get; set; }
public int? ComponentID { get; set; }
public int? CycleID { get; set; }
public int? DaysConnected { get; set; }
public int? DaysInPeriod { get; set; }
public int? DepositTemplateID { get; set; }
public int? DiscountComponentID { get; set; }
public int? DiscountProductComponentID { get; set; }
public int? DisperseWeight { get; set; }
public int? DivisionID { get; set; }
public int? ERateID { get; set; }
public int? ExemptionTypeID { get; set; }
public int? FractionalIsWaivable { get; set; }
public int? GLMapID { get; set; }
public int? GroupQuantity { get; set; }
public int? InitialCycleScheduleID { get; set; }
public int? IsCredit { get; set; }
public int? ItemID { get; set; }
public int? ItemPriceID { get; set; }
public int? LocationID { get; set; }
public int? NumberOfRecurrences { get; set; }
public int? Occurrence { get; set; }
public int? ParentItemID { get; set; }
public int? ParentProductComponentID { get; set; }
public int? PatronageTypeID { get; set; }
public int? PCode { get; set; }
public int? PriceID { get; set; }
public int? PriceListID { get; set; }
public int? PricePlanID { get; set; }
public int? PriceStepID { get; set; }
public int? PriceStepScheduleID { get; set; }
public int? PriceStepTierID { get; set; }
public int? PriceWeight { get; set; }
public int? ProductID { get; set; }
public int? ProviderID { get; set; }
public int? ReceivableID { get; set; }
public int? ReportAreaID { get; set; }
public int? RevenueGLID { get; set; }
public int? RootItemID { get; set; }
public int? RootProductComponentID { get; set; }
public int? RunningQuantityEnd { get; set; }
public int? RunningQuantityStart { get; set; }
public int? SalesRegionID { get; set; }
public int? SegmentID { get; set; }
public int? ServiceID { get; set; }
public int? StepMaxQuantity { get; set; }
public int? StepMinQuantity { get; set; }
public int? TaxAreaID { get; set; }
public int? TaxChargeTypeID { get; set; }
public int? TaxCountType { get; set; }
public int? TotalDisperseWeight { get; set; }
public string AccountUID { get; set; }
public string BillingFrequency { get; set; }
public string BillingMethod { get; set; }
public string ChargeClass { get; set; }
public string ChargeDescription { get; set; }
public string ChargeType { get; set; }
public string FractionalizationMethod { get; set; }
public string ItemStatus { get; set; }
public string PriceScheduleAlignment { get; set; }
public string PriceScheduleBehavior { get; set; }
public string PrintMethod { get; set; }
public string QuoteDescription { get; set; }
public string ReceivableAccount { get; set; }
public string RevenueAccount { get; set; }
public string RoundingMethod { get; set; }
public string TaxServType { get; set; }
public string TaxTranType { get; set; }
public string WaiveFractional { get; set; }
public string WaiverMethod { get; set; }
}
如何接受常规对象
,然后在其上进行模式匹配,如:
public class MyController : ApiController
{
[Route("Order")]
public object Post([FromBody] object request) //note the change to object type rather than Order type
{
switch(object)
{
case Order order when order.noOfWidgets == 1: return Cat();
case Order order when order.noOfWidgets == 0: return Dog();
case Order order when order.noOfWidgets == 3 && TaxTranType == "something": return Elephant();
}
return new List<MyResponse>();
}
}
公共类MyController:ApiController
{
[路线(“订单”)]
public object Post([FromBody]object request)//注意对对象类型而不是订单类型的更改
{
开关(对象)
{
当Order.noOfWidgets==1时的case-Order顺序:返回Cat();
当Order.noOfWidgets==0时的案例顺序:return Dog();
当Order.noOfWidgets==3&&TaxTranType==something时的案例顺序:return Elephant();
}
返回新列表();
}
}
您能否从api控制器将对象
模式匹配为具体对象,如订单
,然后根据模式匹配确定返回哪种类型?
您能否将api控制器中的对象
模式匹配为具体对象,如订单
,然后根据模式匹配确定要返回的类型
假设request
是Order
的一个实例,那么模式匹配就是这样工作的
C#7的模式匹配相当有限,但它支持类型模式,类型模式匹配,x是ty
或switch(x)。。。如果x
类型为T
,则案例T y
然而,我不确定这是你想要做的。如果request
是从HTML创建的任意对象,那么模式匹配不会为您将其转换为Order
。因此,如果这就是你所追求的,那么答案是“不”
您能否将api控制器中的对象
模式匹配为具体对象,如订单
,然后根据模式匹配确定要返回的类型
假设request
是Order
的一个实例,那么模式匹配就是这样工作的
C#7的模式匹配相当有限,但它支持类型模式,类型模式匹配,x是ty
或switch(x)。。。如果x
类型为T
,则案例T y
然而,我不确定这是你想要做的。如果
request
是从HTML创建的任意对象,那么模式匹配不会为您将其转换为Order
。因此,如果这就是您所追求的,那么答案是“否”。在开关之前添加订单订单=请求作为订单
(并检查是否为空)
?@AleksAndreev谢谢,但是如果它是一个不同的对象,例如,不是订单,我想将Bill作为请求传入,那么它就是一个完全不同的对象,我假设您的Post
方法不能处理任何类型的对象(例如int
)。因此,一个解决方案是定义一个接口,该接口将提供某种契约。然后尝试转换到该界面并执行您的开关
谢谢。绝对正确,但是,我不想依赖于接口,我想尽可能地简化,在控制器内部的路由逻辑中具有所有的复杂性,我不想为此创建一个单独的接口将请求类型更改为动态而不是对象是否解决了您的问题?在开关之前添加Order Order=request as Order
(并检查null)如何?@AleksAndreev谢谢,但是如果它是另一个对象,例如,而不是Order,我想通过Bill作为请求,那么这是一个完全不同的对象我假设你的Post
方法不能处理任何类型的对象(int
)。因此,一个解决方案是定义一个接口,该接口将提供某种契约。然后尝试转换到该界面并执行您的开关
谢谢。绝对正确,但是,我不想依赖于接口,我想尽可能地简化,并且在控制器内部的路由逻辑中具有所有的复杂性,我不想为此创建单独的接口将请求类型更改为动态而不是对象解决了您的问题吗?
public class Order
{
public int noOfWidgets {get;set;}
public bool IsSelected { get; set; }
public bool? DisperseCharges { get; set; }
public bool? IsAdjustable { get; set; }
public bool? IsPackageChild { get; set; }
public byte ChargeTypeID { get; set; }
public byte PriceClass { get; set; }
public DateTime BeginDate { get; set; }
public DateTime ChargeBeginDate { get; set; }
public DateTime ChargeEndDate { get; set; }
public DateTime PeriodBeginDate { get; set; }
public DateTime PeriodEndDate { get; set; }
public DateTime QuoteDate { get; set; }
public DateTime? EndDate { get; set; }
public DateTime? PurchaseDate { get; set; }
public DateTime? StepBeginDate { get; set; }
public DateTime? StepEndDate { get; set; }
public decimal? AgentCost { get; set; }
public decimal? Amount { get; set; }
public decimal? BeginRecurs { get; set; }
public decimal? BilledAmount { get; set; }
public decimal? BookedCost { get; set; }
public decimal? ChargeBeginRecurs { get; set; }
public decimal? ChargeEndRecurs { get; set; }
public decimal? ChargeQuantity { get; set; }
public decimal? ChargeRecurs { get; set; }
public decimal? DisperseAmount { get; set; }
public decimal? EndRecurs { get; set; }
public decimal? ETLCost { get; set; }
public decimal? Factor { get; set; }
public decimal? Multiplier { get; set; }
public decimal? NonPayCharge { get; set; }
public decimal? NonPayRate { get; set; }
public decimal? NumberOfPeriodsBilled { get; set; }
public decimal? OtherCost { get; set; }
public decimal? RealCost { get; set; }
public decimal? ScheduleBeginRecurs { get; set; }
public decimal? ScheduleEndRecurs { get; set; }
public decimal? ScheduleTotalRecurs { get; set; }
public decimal? SPIFFCost { get; set; }
public decimal? StandardCharge { get; set; }
public decimal? StandardRate { get; set; }
public decimal? SuspendCharge { get; set; }
public decimal? SuspendRate { get; set; }
public Guid RequestUID { get; set; }
public int ChargeClassID { get; set; }
public int CycleScheduleID { get; set; }
public int ElementClassID { get; set; }
public int ID { get; set; }
public int ProductComponentID { get; set; }
public int Quantity { get; set; }
public int QuoteID { get; set; }
public int YYYYMMCCID { get; set; }
public int? AccountID { get; set; }
public int? ARItemID { get; set; }
public int? BillingChargeClassID { get; set; }
public int? BillingFrequencyMask { get; set; }
public int? BillingNumber { get; set; }
public int? ChargeDays { get; set; }
public int? ChargeIsWaivable { get; set; }
public int? ComponentClassID { get; set; }
public int? ComponentID { get; set; }
public int? CycleID { get; set; }
public int? DaysConnected { get; set; }
public int? DaysInPeriod { get; set; }
public int? DepositTemplateID { get; set; }
public int? DiscountComponentID { get; set; }
public int? DiscountProductComponentID { get; set; }
public int? DisperseWeight { get; set; }
public int? DivisionID { get; set; }
public int? ERateID { get; set; }
public int? ExemptionTypeID { get; set; }
public int? FractionalIsWaivable { get; set; }
public int? GLMapID { get; set; }
public int? GroupQuantity { get; set; }
public int? InitialCycleScheduleID { get; set; }
public int? IsCredit { get; set; }
public int? ItemID { get; set; }
public int? ItemPriceID { get; set; }
public int? LocationID { get; set; }
public int? NumberOfRecurrences { get; set; }
public int? Occurrence { get; set; }
public int? ParentItemID { get; set; }
public int? ParentProductComponentID { get; set; }
public int? PatronageTypeID { get; set; }
public int? PCode { get; set; }
public int? PriceID { get; set; }
public int? PriceListID { get; set; }
public int? PricePlanID { get; set; }
public int? PriceStepID { get; set; }
public int? PriceStepScheduleID { get; set; }
public int? PriceStepTierID { get; set; }
public int? PriceWeight { get; set; }
public int? ProductID { get; set; }
public int? ProviderID { get; set; }
public int? ReceivableID { get; set; }
public int? ReportAreaID { get; set; }
public int? RevenueGLID { get; set; }
public int? RootItemID { get; set; }
public int? RootProductComponentID { get; set; }
public int? RunningQuantityEnd { get; set; }
public int? RunningQuantityStart { get; set; }
public int? SalesRegionID { get; set; }
public int? SegmentID { get; set; }
public int? ServiceID { get; set; }
public int? StepMaxQuantity { get; set; }
public int? StepMinQuantity { get; set; }
public int? TaxAreaID { get; set; }
public int? TaxChargeTypeID { get; set; }
public int? TaxCountType { get; set; }
public int? TotalDisperseWeight { get; set; }
public string AccountUID { get; set; }
public string BillingFrequency { get; set; }
public string BillingMethod { get; set; }
public string ChargeClass { get; set; }
public string ChargeDescription { get; set; }
public string ChargeType { get; set; }
public string FractionalizationMethod { get; set; }
public string ItemStatus { get; set; }
public string PriceScheduleAlignment { get; set; }
public string PriceScheduleBehavior { get; set; }
public string PrintMethod { get; set; }
public string QuoteDescription { get; set; }
public string ReceivableAccount { get; set; }
public string RevenueAccount { get; set; }
public string RoundingMethod { get; set; }
public string TaxServType { get; set; }
public string TaxTranType { get; set; }
public string WaiveFractional { get; set; }
public string WaiverMethod { get; set; }
}
public class MyController : ApiController
{
[Route("Order")]
public object Post([FromBody] object request) //note the change to object type rather than Order type
{
switch(object)
{
case Order order when order.noOfWidgets == 1: return Cat();
case Order order when order.noOfWidgets == 0: return Dog();
case Order order when order.noOfWidgets == 3 && TaxTranType == "something": return Elephant();
}
return new List<MyResponse>();
}
}