简体   繁体   English

如何返回特定的错误信息

[英]how to return specific error message

I have WebAPI based project. 我有基于WebAPI的项目。 It has an API for user registration. 它具有用于用户注册的API。 RegistrationModel looks as below. RegistrationModel如下所示。

public class RegistrationTO
{

    [Required(ErrorMessage="Email required")]
    public string Email { get; set; }

    [Required(ErrorMessage="Name required")]
    public string Name { get; set; }

    [Required(ErrorMessage="Surname required")]
    public string Surname { get; set; }

}

API API

 public string Post([FromBody]RegistrationTO model)
    {
        if (ModelState.IsValid)
        {
           //actual stuff
        }
        else
        {
          return "all * marked fields are required";
        }
    }

My requirement if model have Email as null, I should return message "email required", if Name is null then "Name required" & so on. 我的要求是,如果模型的Email为null,则应返回消息“ email required”,如果Name为null,则返回“ Name required”,依此类推。

Yes,one patchy way would be :- 是的,一种修补方式是:-

public string Post([FromBody]UserRegistrationTO model)
    {
        if (ModelState.IsValid)
        {
           //actual stuff
        }
        else
        {
          if(model.Email == null)
          return "Email required";
          else if(model.Name == null)
           return "name required"; // & so on.
        }
    }

Is there any other simple way to do this?? 还有其他简单的方法吗?

try do that: 尝试这样做:

        if (ModelState.IsValid)
        {
           // do something
        }
        else
            return "The following errors happen: \n" +
            ModelState.SelectMany(s => s.Value.Errors)
            .Select(s => s.ErrorMessage)
            .Aggregate((seed, currentValue) => seed += "\n" + currentValue);

I hope it can help you. 希望对您有所帮助。

You can do this at the view itself. 您可以在视图本身上执行此操作。

For individual properties : 对于单个属性:

@Html.ValidationMessageFor(x => x.Email, null, new { @class = "text-danger" })

and for all failed validation 对于所有失败的验证

@if (!ViewData.ModelState.IsValid)
{
   <div class="alert alert-danger">
     @Html.ValidationSummary(false)
   </div>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回带有详细错误消息的错误? - How to return an error with detailed error message? 如何将自定义错误消息返回给View? - How to return custom error message to View? 如果轨迹栏的输入为偶数,如何返回错误消息? - How to return error message if input of trackbar is even? 如何使用 Fluent Validator 在 RuleForEach 验证的消息中返回特定属性? - How to return a specific property in the message of a RuleForEach validation, using Fluent Validator? 如何在不同的返回类型函数中返回错误消息? - How do i return error message in different return type function? 使用actionResult返回错误消息 - return error message with actionResult 如何创建一个存储过程,该过程返回错误消息并使用前端的消息框打印该错误消息 - How to create a stored procedure that return error message and print that error message using message box from front end 如何通过 Ajax 返回无效密码错误消息 - MVC - How to return invalid password error message via Ajax - MVC 如何验证多个数据注释并在视图中返回一条错误消息 - How to validate multiple data annotations and return one error message in view 如何将错误消息从C#属性设置器“返回”给用户? - How to “return” an error message from a C# property setter to the user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM