如何在 .net WebAPI 中验证只有 3 个可能值的字符串值?
How to validate in a .net WebAPI a string value with only 3 possible values?
我知道这个问题看起来很简单,但我想知道是否有比做更好的方法
if (partner.Type =="value" 或 value2 或 value3)
也许有数据注释?
这是我的实体 POCO
/// <summary>
/// Partner Entity
/// </summary>
[SharedCosmosCollection("shared")]
public class Partner : ISharedCosmosEntity
{
/// <summary>
/// Partner id
/// </summary>
[JsonProperty("Id")]
public string Id { get; set; }
/// <summary>
/// Partner name
/// </summary>
public string PartnerName { get; set; }
/// <summary>
/// Partner contact name
/// </summary>
public string PartnerContact { get; set; }
/// <summary>
/// Partner contact phone
/// </summary>
public string PartnerPhone { get; set; }
/// <summary>
/// Partner contact Office 365 domain
/// </summary>
public string PartnerDomain { get; set; }
/// <summary>
/// Partner type, silver, gold or platinum
/// </summary>
public string PartnerType { get; set; }
/// <summary>
/// Partner start date
/// </summary>
public DateTime StartDate { get; set; }
/// <summary>
/// Partner end date
/// </summary>
public DateTime EndDate { get; set; }
/// <summary>
/// Parter enabled
/// </summary>
public bool Enabled { get; set; }
[CosmosPartitionKey]
public string CosmosEntityName { get; set; }
}
这是我的控制器
[HttpPost]
public async Task<IHttpActionResult> AddPartner([FromBody]Partner partner)
{
var telemetry = new TelemetryClient();
try
{
var partnerStore = CosmosStoreHolder.Instance.CosmosStorePartner;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var added = await partnerStore.AddAsync(partner);
return Ok(added);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
使用正则表达式验证器作为 属性 上的数据注释。
[RegularExpression("^silver$|^gold$|^platinum$", ErrorMessage = "Invalid Value")]
public string PartnerType { get; set; }
P.S。正则表达式可能需要调整,因为我只是在脑海中输入它以匹配您提到的可能值。
我知道这个问题看起来很简单,但我想知道是否有比做更好的方法
if (partner.Type =="value" 或 value2 或 value3)
也许有数据注释?
这是我的实体 POCO
/// <summary>
/// Partner Entity
/// </summary>
[SharedCosmosCollection("shared")]
public class Partner : ISharedCosmosEntity
{
/// <summary>
/// Partner id
/// </summary>
[JsonProperty("Id")]
public string Id { get; set; }
/// <summary>
/// Partner name
/// </summary>
public string PartnerName { get; set; }
/// <summary>
/// Partner contact name
/// </summary>
public string PartnerContact { get; set; }
/// <summary>
/// Partner contact phone
/// </summary>
public string PartnerPhone { get; set; }
/// <summary>
/// Partner contact Office 365 domain
/// </summary>
public string PartnerDomain { get; set; }
/// <summary>
/// Partner type, silver, gold or platinum
/// </summary>
public string PartnerType { get; set; }
/// <summary>
/// Partner start date
/// </summary>
public DateTime StartDate { get; set; }
/// <summary>
/// Partner end date
/// </summary>
public DateTime EndDate { get; set; }
/// <summary>
/// Parter enabled
/// </summary>
public bool Enabled { get; set; }
[CosmosPartitionKey]
public string CosmosEntityName { get; set; }
}
这是我的控制器
[HttpPost]
public async Task<IHttpActionResult> AddPartner([FromBody]Partner partner)
{
var telemetry = new TelemetryClient();
try
{
var partnerStore = CosmosStoreHolder.Instance.CosmosStorePartner;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var added = await partnerStore.AddAsync(partner);
return Ok(added);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
使用正则表达式验证器作为 属性 上的数据注释。
[RegularExpression("^silver$|^gold$|^platinum$", ErrorMessage = "Invalid Value")]
public string PartnerType { get; set; }
P.S。正则表达式可能需要调整,因为我只是在脑海中输入它以匹配您提到的可能值。