解码 JSON 命令行参数在构建后失败但在调试期间成功

Decoding JSON command line arguments fails after build but succeeds during debug

我正在构建一个 C# 应用程序来处理我的 Web 应用程序中使用的自定义协议。

link 就像:

<a href="zebra-wp://%7B%22barcode%22%3A63%2C%22name%22%3A%22Food%20Fun%20-%20Magnetic%20Multicultural%20set%22%7D">Print</a>

这些是使用 windows 注册表中的处理程序处理的(URL:zebra-wp 协议):

"C:\Program Files (x86)\[My App Name]\[My App].exe" "%1"

我是 运行 我的应用程序中的以下代码:

class LabelData
    {
        public string name;
        public string barcode;
    }

    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length > 0 && args[0].StartsWith("zebra-wp://"))
            {

                // retrieve data from argument
                string argData = args[0].Remove(0, 11);

                string decodedJson = "";
                try
                {
                    // Undo URL Encoding
                    decodedJson = WebUtility.UrlDecode(argData);
                }
                catch (Exception ex)
                {
                    string msg = "Couldn't print label, failed to decode data.";
                    msg += "\nData: " + argData;
                    msg += "Error: " + ex.Message;
                    MessageBox.Show(msg);
                    Application.Exit();
                }

                // Unpack JSON string
                LabelData decodedData = new LabelData();
                try
                {
                    decodedData = JsonConvert.DeserializeObject<LabelData>(decodedJson);
                }
                catch (Exception ex)
                {
                    string msg = "Couldn't print label, failed to unpack data.";
                    msg += "\nData: " + decodedJson;
                    msg += "Error: " + ex.Message;
                    MessageBox.Show(msg);
                    Application.Exit();
                }

                // Do things with object

当我调试应用程序时,我在“命令行参数”启动选项中输入 link URL。 程序按预期工作,JSON 数据成功解码。

当我构建和安装时,JsonConvert.DeserializeObject 函数给我以下错误:

Data: {"barcode":"000063","name":"Food Fun - Magnetic Multicultural set"}
Error: Unexpected end while parsing comment. Path '', line 1, position 68.

VS 在调试中使用命令行参数启动应用程序的方式有什么不同吗? 有没有一种方法可以使用与单击 URL?

时相同的命令行参数来调试应用程序

我发现了这个问题,显然在将 URI 传递给自定义协议处理程序时,Windows 向 URI 添加了尾部正斜杠,在代码中检查并删除它解决了问题。