Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How force a maximum string length in a C# web service object property?

In this class for example, I want to force a limit of characters the first/last name can allow.

public class Person
{
     public string FirstName { get; set; }
     public string LastName { get; set; }
}

Is there a way to force the string limit restriction for the first or last name, so when the client serializes this before sending it to me, it would throw an error on their side if it violates the lenght restriction?

Update: this needs to be identified and forced in the WSDL itself, and not after I've recieved the invalid data.

like image 756
Joshua Turner Avatar asked Dec 09 '22 22:12

Joshua Turner


1 Answers

necro time... It worth mentioning though.

using System.ComponentModel.DataAnnotations;
public class Person
{
     [StringLength(255, ErrorMessage = "Error")]
     public string FirstName { get; set; }
     [StringLength(255, ErrorMessage = "Error")]
     public string LastName { get; set; }
}
like image 176
Jason Avatar answered Feb 15 '23 09:02

Jason