C#FormatException,不涉及任何数值或空值解析

时间:2015-09-16 17:34:46

标签: c# string-formatting

此行抛出FormatException:

var dataString = string.Format("email_address:{0}, status:subscribed, merge_fields:{1}", "a", "b");

毫无疑问,问题很明显,但我没有看到。

这是功能:

   public static string AddSubscriber(string listId, Subscriber subscriber) {

            string url = string.Format("https://us10.api.mailchimp.com/3.0/lists/{0}/members/", listId);

            var req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Headers.Add("AUTHORIZATION", basicAuth);

            var mergeFieldJson = subscriber.MergeFields();
            var dataString = string.Format("email_address:{0}, status:subscribed, merge_fields:{1}", "a", "b"); // subscriber.email, mergeFieldJson);

            /*
            byte[] data = System.Text.Encoding.UTF8.GetBytes(dataString);
            using (var stream = req.GetRequestStream()) {
                stream.Write(data, 0, data.Length);
            }

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();

            return new StreamReader(response.GetResponseStream()).ReadToEnd();
            */
            return "";
        }

以下是例外:

Input string was not in a correct format. 
    at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    at System.String.Format(IFormatProvider provider, String format, Object[] args)
    at MailChimpLib.MailChimp.AddSubscriber(String listId, Subscriber subscriber) in d:\Development\GTPRepository\team24x7\trunk\MailChimpLib\MailChimp.cs:line 40
    at team24x7.Controllers.StoreController.AddTestSubscriber() in d:\Development\GTPRepository\team24x7\trunk\team24x7\api_source\Controllers\StoreController.cs:line 167

第40行是string.Format(..)。

的行

这是MergeFields函数:

   public string MergeFields() {
        return string.Format("{'Email':'{0}','First Name':'{1}','Last Name':'{2}','Mascot':'{3}','Store Link':'{4}','School':'{5}'}",
            email, firstname, lastname, mascot, storelink, school
        );
    }

对我来说,它在调试模式下工作,但在发布模式下调用它之后的行中会出现异常。

毫无疑问,这个问题与在格式字符串中包含初始和结束花括号的愚蠢和错误的尝试有关。 (愚蠢的诅咒。)

2 个答案:

答案 0 :(得分:2)

我在Ideone中运行了你的代码,它运行正常。

https://ideone.com/cHQC3O

using System;

public class Test
{
    public static void Main()
    {
        var dataString = string.Format("email_address:{0}, status:subscribed, merge_fields:{1}", "a", "b");
        Console.WriteLine(dataString);
    }
}

似乎我们遗漏了代码中未提及的内容。

你有什么例外?什么是例外的消息?

答案 1 :(得分:1)

经过几次评论后,我们找到了问题的根源:

return string.Format("{'Email':'{0}','First Name':'{1}','Last Name':'{2}','Mascot':'{3}','Store Link':'{4}','School':'{5}'}",
    email, firstname, lastname, mascot, storelink, school
);

这里有FormatException,因为你的字符串中有大括号{符号,在形成字符串时有特殊含义。解决方案是使用双花括号{{}}来逃避它,因此您的代码变为:

return string.Format("{{'Email':'{0}','First Name':'{1}','Last Name':'{2}','Mascot':'{3}','Store Link':'{4}','School':'{5}'}}",
    email, firstname, lastname, mascot, storelink, school
);