如何强制 C# Web 服务对象属性中的最大字符串长度?
例如,在此类中,我想强制限制名字/姓氏可以允许的字符数。
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
有没有办法强制对名字或姓氏进行字符串限制,因此当客户端在将其发送给我之前对其进行序列化时,如果违反长度限制,则会在他们这边抛出错误?
更新:这需要在 WSDL 本身中进行识别和强制,而不是在我收到无效数据之后。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
死灵时间……不过值得一提。
necro time... It worth mentioning though.
您可以使用 SOAP 扩展:
You can apply XML Schema validation (e.g., maxLength facet) using SOAP Extensions:
从自动属性转换属性并自己验证它,然后您可以抛出参数异常或类似的东西,他们在提交之前必须处理这些异常。
注意:如果 .NET 以外的语言将调用您最有可能也希望在服务端验证它。 或者进行最小测试,看看它在另一种语言中如何工作。
COnvert the property from an auto property and validate it yourself, you could then throw an argument exception or something similar that they would have to handle before submission.
NOTE: if languages other than .NET will be calling you most likely want to be validating it on the service side as well. Or at minimun test to see how it would work in another language.