0

这可能很简单,但我有一个 .asmx Web 服务,它返回一个序列化为 json 的对象。如果有错误,我想抛出一个字符串。我最终为错误消息添加了一个公共字符串。我可以转换我的对象以返回一个字符串吗?

public TmsObject GetTombstoneDataJson(string ObjectNumber)
{
    if ((Regex.IsMatch(ObjectNumber, @"^([\w\._-]|\s)*$")))
    {
        try
        {
            TmsObject tmsd = new TmsObject();

            tmsd.Dated = "foo";
            tmsd.Medium = "foo";
            tmsd.Dimensions = "foo";
            tmsd.ObjectNumber = "foo";
            tmsd.CreditLine = "foo";

            return tmsd;
        }
        catch
        {       
            TmsObject notmsd = new TmsObject();  
            notmsd.ErrorMsg = "There was an error in processing this request.  Please review the web service description and try again.";
            return notmsd;
        }
    }
    else
    {
        TmsObject notmsd = new TmsObject();
        notmsd.ErrorMsg = "One of the required parameters is empty or incorrect.";
        return notmsd;
    }
}
4

1 回答 1

0

您需要将方法的返回类型更改为string. 但是,我不认为你想这样做。无论如何,ErrorMsg字符串已经作为对象的一部分返回。因此,在继续ErrorMsg之前,由调用者检查是否包含任何内容。

于 2013-05-11T09:02:31.607 回答