你如何检查这个 NegotiatedContentResult
的 IHttpActionResult
是否有 Ok StatusCode
。
前任。 if (ValidateRegistrationStep3(accountModel) == Ok()) { code to run }
var result = ValidateRegistrationStep3(accountModel);
如果你检查
result
中的 Locals
它有一个 Value
的 System.Web.Http.Results.NegotiatedContentResult<string>
Type
System.Web.Http.IHttpActionResult {System.Web.Http.Results.NegotiatedContentResult<string>}
和一个 StatusCode
的 OK
。如何检查 StatusCode
?[HttpPost]
[Route("register/validate3")]
public IHttpActionResult ValidateRegistrationStep3(
[FromBody]AccountRegisterModel accountModel) {
List<string> errors = new List<string>();
if (!IsDigits(accountModel.CreditCard, 16)) errors.Add("CreditCard needs to be 16 digits.");
var regExp = @"^(0[1-9]|1[0-2])\/[0-9]{2}$";
if (!Regex.IsMatch(accountModel.ExpDate, regExp, RegexOptions.IgnoreCase))
errors.Add("Expiration Date is not valid format (MM/YY)");
if (errors.Count == 0) return Content(HttpStatusCode.OK, "Register Model-3 Valid");
else return Content(HttpStatusCode.BadRequest, errors.ToArray()); }
解决方案:
将验证代码分解为辅助方法,然后从
IHttpActionResult
调用它。[HttpPost]
[Route("register/validate3")]
public IHttpActionResult ValidateRegistrationStep3(
[FromBody]AccountRegisterModel accountModel) {
List<string> errors = ValidateRegistrationStep3Inner(accountModel);
if (errors.Count == 0) return Content(HttpStatusCode.OK, "Register Model-3 Valid");
else return Content(HttpStatusCode.BadRequest, errors.ToArray()); }
private List<string> ValidateRegistrationStep3Inner(AccountRegisterModel accountModel)
{
List<string> errors = new List<string>();
if (!IsDigits(accountModel.CreditCard, 16)) errors.Add("CreditCard needs to be 16 digits.");
if (!string.IsNullOrWhiteSpace(accountModel.ExpDate)) {
var regExp = @"^(0[1-9]|1[0-2])\/[0-9]{2}$";
if (!Regex.IsMatch(accountModel.ExpDate, regExp, RegexOptions.IgnoreCase))
errors.Add("Expiration Date is not valid format (MM/YY)");
} else errors.Add("Expiration Date is required (MM/YY)");
return errors; }
然后可以从
Controller
中调用 helper 方法并检查它的返回值。if (ValidateRegistrationStep1Inner(accountModel).Count == 0 &&
ValidateRegistrationStep2Inner(accountModel).Count == 0 &&
ValidateRegistrationStep3Inner(accountModel).Count == 0)
最佳答案
要回答您的问题 - 您需要执行结果。
public async Task<HttpStatusCode> GetResult()
{
var response = ValidateRegistrationStep3(accountModel);
var result = await response.ExecuteAsync(new CancellationToken());
var statusCode = result.StatusCode;
return statusCode;
}
但是您永远不需要自己直接调用 Controller 。相反,将验证代码移动到一个单独的类(或添加一个不涉及
IHttpActionResult
的新方法)。public class RegistrationValidation : IRegistrationValidation
{
public List<string> Validate(AccountRegisterModel model)
{
List<string> errors = new List<string>();
...
return errors;
}
}
public class RegistrationController : ApiController
{
private readonly IRegistrationValidation validator;
// with a DI container
public RegistrationController(IRegistrationValidation validator)
{
this.validator = validator;
}
// without a DI container
public RegistrationController()
{
this.validator = new RegistrationValidation();
}
public IHttpActionResult ValidateRegistrationStep3(AccountRegisterModel accountModel)
{
var errors = validator.Validate(accountModel);
if (error.Count() == 0)
return Content(HttpStatusCode.OK, "Register Model-3 Valid");
...
}
public IHttpActionResult FinishRegistration(AccountRegisterModel accountModel)
{
var errors = validator.Validate(accountModel);
if (errors.Count() == 0)
{
...
}
}
}
关于c# - 获取 NegotiatedContentResult 的 StatusCode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48710962/