【问题标题】:asp.net Json with asmx not working on server It is working just in local?asmx 的 asp.net Json 不能在服务器上工作 它只在本地工作?
【发布时间】:2019-03-01 10:19:07
【问题描述】:

我正在尝试在 asp.net 中编写 webservices Json。这段代码在我的本地电脑上运行。但它在服务器上不起作用。

[WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 公共无效 GetMember() { HttpContext.Current.Response.Write(JsonConvert.SerializeObject(DALMember.GetAll())); }

[NullReferenceException:对象引用未设置为对象的实例。] DOAdverisingModule.AdsModule.context_PostRequestHandlerExecute(对象发送者,EventArgs e)+295 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +71

【问题讨论】:

  • NullReferenceException: Object reference not set to an instance of an object. 逐步检查并找出哪个是空的。
  • 我该怎么做?本地没有问题
  • 嗯,它可能是DALMember,因为错误上方的行指的是DALMember.GetAll()。因此,无论出于何种原因,该对象为空,所以 .GetAll 调用会抛出。
  • 至于为什么它在本地而不是在服务器上工作,它们是否使用相同的数据源?
  • 本地和服务器端都连接了sql server。

标签: c# asp.net json asmx


【解决方案1】:

首先,我建议切换到 MVC API。 Web 服务已是老生常谈,而 MVC API 将为您提供更广泛的可用性。

其次,您遇到的错误是在您单步执行 (.ASMX) 的代码时出现的吗? -- 意思是,您可以在您的网站上执行调用 Web 服务的代码没有问题,只是您尝试在 Web 服务中执行的任何代码都失败了?

【讨论】:

  • 当我点击 GetMember() 并调用它给我这个错误。但在本地它工作正常。我会失去理智
  • 此页面是否可以使用 DALMember.GetAll() 方法?您可能需要先实例化 DALMember,DALMember dm = new DALMember()
  • DALMember.GetAll() 返回成员列表
  • 你有使用Newtonsoft.Json的参考;?
  • 尝试 DALMember m = new DALMember(); HttpContext.Current.Response.Write(JsonConvert.SerializeObject(m.GetAll()));
猜你喜欢
  • 2015-01-16
  • 2019-11-08
  • 2016-10-16
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多