C# &引用;对象引用未设置为对象的实例;使用JQuery/AJAX将JSON发送到Web服务时

C# &引用;对象引用未设置为对象的实例;使用JQuery/AJAX将JSON发送到Web服务时,c#,javascript,jquery,ajax,json,C#,Javascript,Jquery,Ajax,Json,编辑:在弄清楚如何单步执行代码后,DataManagement.dll中出现了类型为“System.NullReferenceException”的异常,但用户代码中未处理该异常。它发生在这一行: string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf"); string path = HttpContext.Current.Server.MapPa

编辑:在弄清楚如何单步执行代码后,DataManagement.dll中出现了类型为“System.NullReferenceException”的异常,但用户代码中未处理该异常。它发生在这一行:

string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
我不确定这意味着什么,但希望这能让我们对这个问题有更多的了解

我试图将一个JavaScript对象发送到一个web服务,但得到了500个错误:“对象引用未设置为对象的实例”

我可以肯定的是,我正在向web服务中的方法传递一些格式错误的内容,但我不是很确定。任何我能得到的帮助都会很棒。我是一个初学者,在这个问题上我的头一直撞在墙上

JSON(来自fiddler):

Javascript:

var labelInfo = new Object();
labelInfo.sampleIds = sampleIds;
labelInfo.line1 = $('#ddlGrower').val();
labelInfo.line2 = $('#ddlFarm').val();
labelInfo.line3 = $('#ddlField1').val();
labelInfo.labelType = $('#ddlSoilLoginMatrix option:selected').text();
labelInfo.startingLabelPosition = parseInt($('#labelSelection').text());

$.ajax({
    type: "POST",
    url: "DesktopModules/DataManagement/TestService.svc/CreateLabelPdf",
    data: JSON.stringify(labelInfo),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        window.open(data.d);
    },
    error:  function(msg) {
        alert("Error: " + msg.status);
    }
});
C#web服务:

[OperationContract]
public string CreateLabelPdf(List<string> sampleIds, string line1, string line2, string line3, string labelType, int startingLabelPosition)
{
   List<LabelContent> labels = new List<LabelContent>();

   foreach (var sample in sampleIds)
   {
      LabelContent labelContent = new LabelContent();
      labelContent.Line1 = line1;
      labelContent.Line2 = line2;
      labelContent.Line3 = line3;
      labelContent.LabelId = sample;
      labels.Add(labelContent);
   }   

   Creator creator = new Creator
   {
      IncludeLabelBorders = false
   };
   string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
   creator.PrintLabels(labels, new Avery5160(), path, startingLabelPosition);
   return path;
}
[运营合同]
公共字符串CreateLabelPdf(列出样本ID、字符串行1、字符串行2、字符串行3、字符串labelType、int startingLabelPosition)
{
列表标签=新列表();
foreach(样本ID中的var样本)
{
LabelContent LabelContent=新的LabelContent();
labelContent.Line1=Line1;
labelContent.Line2=Line2;
labelContent.Line3=第3行;
labelContent.LabelId=样本;
标签。添加(标签内容);
}   
创建者=新的创建者
{
includeLabelOrders=false
};
字符串路径=HttpContext.Current.Server.MapPath(@“~\DesktopModules\DataManagement\Pdf\”+0+“.Pdf”);
creator.PrintLabels(标签,新Avery5160(),路径,startingLabelPosition);
返回路径;
}

JSON似乎无效。我认为应该从以下方面开始:

{"sampleIds":["1"],"line1":"","line2":"","line3":"","labelType":"Soil","startingLabelPosition":1}
现在,
sampleid
是这样的:

{"sampleIds:["1"],"line1":"","line2":"","line3":"","labelType":"Soil","startingLabelPosition":1}

也就是说,
“sampleid”
没有正确地格式化为键。

所以我终于找到了答案。我调试了代码,发现错误在这一行:

string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
这是一个空引用错误,因此我将代码更改为:

string path = System.Web.Hosting.HostingEnvironment.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
这是可行的,但我需要将相对字符串返回到浏览器,而不是物理路径,因此我加入了:

string relativePath = @"\DesktopModules\DataManagement\Pdf\0.pdf";
return relativePath;
结束代码如下所示:

string path = System.Web.Hosting.HostingEnvironment.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");

creator.PrintLabels(labels, new Avery5160(), path, startingLabelPosition);

string relativePath = @"\DesktopModules\DataManagement\Pdf\0.pdf";
return relativePath;

@史密斯·h·尼尔啊,对不起,那只是一个人为错误。在哪一行发生了错误?@ArtyomNeustroev,我不确定?那是堆栈跟踪吗?在那之后也会持续相当长一段时间
StackTrace=at Mvtl.Website.Modules.DataManagement.TestService.CreateLabelPdf(列表1样本ID、字符串行1、字符串行2、字符串行3、字符串标签类型、Int32 startingLabelPosition)在\\10.8.1.9\c$\sites\www.website.com\DesktopModules\DataManagement\TestService.svc.cs中:第380行
放置一个断点,然后查看发生了什么。@inanikian我不确定如何在visual studio中使用远程服务器上的代码执行此操作。我已经在浏览器web工具中为javascript做过了,但是是的。我想我必须附加一些过程?我不确定。那是我用正确的JSON格式fiddler编辑的那部分的输入错误。你试过调试哪个变量为null吗?嗯,没有?在VisualStudio中,我试图开始调试,但出现了一个错误,说它无法添加服务?我想我调试了代码。我在上面编辑了它。错误在路径字符串中,但我不确定如何前进。