locked
How to show validation message while user give invalid data or wrong URL? RRS feed

  • Question

  • User-147238865 posted

    Hi Panel;

    I have created a web API method called as GETStudedntDetails and I am returning value by JSON.

    Bellow is my method;

    public HttpResponseMessage Get(string id,string name)
    {
    string errormessage=""
    if(id==null)
    {
    errormessage="ENter ID";
    flag=true
    }
    if(name==null)
    {
    errormessage="ENter name";
    flag=true;
    }
    if(flag)
    {
    return content=new httpResponsemessage{
    errormesage=errormessage
    }
    }
    
    
    }

    In the above code I am checking with null value and if condtion satisfy I am returning error message .

    But in JSON format the out put coming like this;

    "ErrorMessage" : "Enter StudentName. Enter StudentRoll".

    Is it possible to place each error separate line; and is it correct approach to do this ?

    Thanks;

    SK.

    Monday, January 8, 2018 5:11 AM

All replies

  • User283571144 posted

    Hi SURYA_TECH,

    In the above code I am checking with null value and if condtion satisfy I am returning error message .

    But in JSON format the out put coming like this;

    "ErrorMessage" : "Enter StudentName. Enter StudentRoll".

    Is it possible to place each error separate line; and is it correct approach to do this ?

    According to your description, I suggest you could try to using use attributes from the System.ComponentModel.DataAnnotations namespace to set validation rules for properties on your model.

    For example:

    You could create a model named produce with validation rules for properties.

        public class Product
        {
            public int Id { get; set; }
            [Required(ErrorMessage = "You forgot to enter a username.")]
            public string Name { get; set; }
            public decimal Price { get; set; }
            [Range(0, 999)]
           
            public double Weight { get; set; }
        }

    Then you could create an action filter to check the model state before the controller action is invoked. 

        public class ValidateModelAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(HttpActionContext actionContext)
            {
                if (actionContext.ModelState.IsValid == false)
                {
                    actionContext.Response = actionContext.Request.CreateErrorResponse(
                        HttpStatusCode.BadRequest, actionContext.ModelState);
                }
            }
        }

    The action like this:

        public class ProductsController : ApiController
        {
            public HttpResponseMessage Post(Product product)
            {
                if (ModelState.IsValid)
                {
                    // Do something with the product (not shown).
    
                    return new HttpResponseMessage(HttpStatusCode.OK);
                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                }
            }
        }

    The result:

    More details, you could refer to this article.

    Best Regards,

    Brando

    Tuesday, January 9, 2018 2:20 AM