ASP.NET Web API为非GET请求提供空引用错误

时间:2013-11-11 05:00:28

标签: asp.net-web-api

我有一个ApiController,可以接收并响应HTTP Get请求,但是当我尝试使用单个Guid或String参数发布或删除任何内容时,我从框架内获得了一个空引用异常。

令人沮丧的是,这可以在我的本地计算机上运行,​​但是当我在服务器上运行它时会开始出现此错误。

我在这里做错了什么:

控制器代码:

<Authorize()>
Public Class WatchlistController
   Inherits ApiController

   Private ReadOnly watchlistTask As WatchlistTask
   Private ReadOnly logger As ILog

   Public Sub New(watchlistTask As WatchlistTask, logger As ILog)
      logger.DebugFormat("Creating controller with task: {0} and logger: {1}", watchlistTask, logger)
      Me.watchlistTask = watchlistTask
      Me.logger = logger
   End Sub

   <HttpGet()>
   Public Function Test() As String
      Return "Hello World"
   End Function

   <HttpGet()>
   Public Function TestReturn(test As String) As String
      Return test
   End Function

   <HttpPost()>
   Public Function AddItem(<FromUri()> itemId As String) As ItemDto
      Dim username = User.Identity.Name
      logger.DebugFormat("Adding item ID {0} to watchlist for {1}", securityId, username)
      Return watchlistTask.AddItemToWatchlist(username, New Guid(itemId))
   End Function

   <HttpDelete()>
   Public Sub RemoveItem(<FromUri()> itemId As Guid)
      Dim username = User.Identity.Name
      logger.DebugFormat("Removing item ID {0} from watchlist for {1}", securityId, username)
      watchlistTask.RemoveItemFromWatchlist(username, itemId)
   End Sub
End Class

致电代码:

$.ajax({ 
   url: 'api/Watchlist/RemoveItem?itemId=' + itemId, 
   type: 'DELETE' 
})
.success(function () { displaySuccess(); })
.error(function () { displayError(); });

and

$.post('api/Watchlist/AddItem?itemId=' + itemId,
      function (itemDto) { displaySuccess(itemDto); }
)
.fail(function () { displayError(); });

上面的Test()和TestReturn()方法有效,但AddItem和RemoveItem不起作用。 日志表明操作和参数被正确找到,但随后抛出NullReference异常:

INFO  Log4NetTraceWriter POST System.Web.Http.Request http://server/api/Watchlist/AddItem?itemId=BCA077A7-F5C2-4B51-9325-1D30FA2116C0
INFO  Log4NetTraceWriter POST System.Web.Http.Controllers DefaultHttpControllerSelector SelectController Route='controller:Watchlist,action:AddItem'
INFO  Log4NetTraceWriter POST System.Web.Http.Controllers DefaultHttpControllerSelector SelectController Watchlist
INFO  Log4NetTraceWriter POST System.Web.Http.Controllers HttpControllerDescriptor CreateController
DEBUG WatchlistController Creating controller with task: WatchlistTask and logger: log4net.Core.LogImpl
INFO  Log4NetTraceWriter POST System.Web.Http.Controllers HttpControllerDescriptor CreateController WatchlistController
INFO  Log4NetTraceWriter POST System.Web.Http.Controllers WatchlistController ExecuteAsync
INFO  Log4NetTraceWriter POST System.Web.Http.Action ApiControllerActionSelector SelectAction
INFO  Log4NetTraceWriter POST System.Web.Http.Action ApiControllerActionSelector SelectAction Selected action 'AddItem(String itemId)'
INFO  Log4NetTraceWriter POST System.Web.Http.ModelBinding HttpActionBinding ExecuteBindingAsync
INFO  Log4NetTraceWriter POST System.Web.Http.ModelBinding ModelBinderParameterBinding ExecuteBindingAsync Binding parameter 'itemId'
INFO  Log4NetTraceWriter POST System.Web.Http.ModelBinding ModelBinderParameterBinding ExecuteBindingAsync Parameter 'itemId' bound to the value 'BCA077A7-F5C2-4B51-9325-1D30FA2116C0'
INFO  Log4NetTraceWriter POST System.Web.Http.ModelBinding HttpActionBinding ExecuteBindingAsync Model state is valid. Values: itemId=BCA077A7-F5C2-4B51-9325-1D30FA2116C0
INFO  Log4NetTraceWriter POST System.Web.Http.Action ApiControllerActionInvoker InvokeActionAsync Action='AddItem(itemId=BCA077A7-F5C2-4B51-9325-1D30FA2116C0)'
INFO  Log4NetTraceWriter POST System.Web.Http.Action ReflectedHttpActionDescriptor ExecuteAsync Invoking action 'AddItem(itemId=BCA077A7-F5C2-4B51-9325-1D30FA2116C0)'
ERROR Log4NetTraceWriter POST System.Web.Http.Action ReflectedHttpActionDescriptor ExecuteAsync Object reference not set to an instance of an object.

ERROR Log4NetTraceWriter POST System.Web.Http.Action ApiControllerActionInvoker InvokeActionAsync Object reference not set to an instance of an object.

ERROR Log4NetTraceWriter POST System.Web.Http.Controllers WatchlistController ExecuteAsync Object reference not set to an instance of an object.

INFO  Log4NetTraceWriter POST System.Net.Http.Formatting DefaultContentNegotiator Negotiate Type='HttpError', formatters=[JsonMediaTypeFormatterTracer, XmlMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer]
INFO  Log4NetTraceWriter POST System.Net.Http.Formatting JsonMediaTypeFormatter GetPerRequestFormatterInstance Obtaining formatter of type 'JsonMediaTypeFormatter' for type='HttpError', mediaType='application/json; charset=utf-8'
INFO  Log4NetTraceWriter POST System.Net.Http.Formatting JsonMediaTypeFormatter GetPerRequestFormatterInstance Will use same 'JsonMediaTypeFormatter' formatter
INFO  Log4NetTraceWriter POST System.Net.Http.Formatting DefaultContentNegotiator Negotiate Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'
INFO  Log4NetTraceWriter POST System.Web.Http.Request Content-type='application/json; charset=utf-8', content-length=unknown
INFO  Log4NetTraceWriter POST System.Net.Http.Formatting JsonMediaTypeFormatter WriteToStreamAsync Value='System.Web.Http.HttpError', type='HttpError', content-type='application/json; charset=utf-8'
INFO  Log4NetTraceWriter POST System.Net.Http.Formatting JsonMediaTypeFormatter WriteToStreamAsync
INFO  Log4NetTraceWriter POST System.Web.Http.Controllers WatchlistController Dispose
INFO  Log4NetTraceWriter POST System.Web.Http.Controllers WatchlistController Dispose

和删除:

INFO DELETE System.Web.Http.Request http://server/api/Watchlist/RemoveItem?itemId=1E7755E5-24A3-4A03-860A-55AA13F7A118
INFO DELETE System.Web.Http.Controllers DefaultHttpControllerSelector SelectController Route='controller:Watchlist,action:RemoveItem'
INFO DELETE System.Web.Http.Controllers DefaultHttpControllerSelector SelectController Watchlist
INFO DELETE System.Web.Http.Controllers HttpControllerDescriptor CreateController
DEBUG WatchlistController Creating controller with task: WatchlistTask and logger: log4net.Core.LogImpl
INFO DELETE System.Web.Http.Controllers HttpControllerDescriptor CreateController WatchlistController
INFO DELETE System.Web.Http.Controllers WatchlistController ExecuteAsync
INFO DELETE System.Web.Http.Action ApiControllerActionSelector SelectAction
INFO DELETE System.Web.Http.Action ApiControllerActionSelector SelectAction Selected action 'RemoveItem(Guid itemId)'
INFO DELETE System.Web.Http.ModelBinding HttpActionBinding ExecuteBindingAsync
INFO DELETE System.Web.Http.ModelBinding ModelBinderParameterBinding ExecuteBindingAsync Binding parameter 'itemId'
INFO DELETE System.Web.Http.ModelBinding ModelBinderParameterBinding ExecuteBindingAsync Parameter 'itemId' bound to the value '1E7755E5-24A3-4A03-860A-55AA13F7A118'
INFO DELETE System.Web.Http.ModelBinding HttpActionBinding ExecuteBindingAsync Model state is valid. Values: itemId=1E7755E5-24A3-4A03-860A-55AA13F7A118
INFO DELETE System.Web.Http.Action ApiControllerActionInvoker InvokeActionAsync Action='RemoveItem(itemId=1E7755E5-24A3-4A03-860A-55AA13F7A118)'
INFO DELETE System.Web.Http.Action ReflectedHttpActionDescriptor ExecuteAsync Invoking action 'RemoveItem(itemId=1E7755E5-24A3-4A03-860A-55AA13F7A118)'
ERROR DELETE System.Web.Http.Action ReflectedHttpActionDescriptor ExecuteAsync Object reference not set to an instance of an object.

ERROR DELETE System.Web.Http.Action ApiControllerActionInvoker InvokeActionAsync Object reference not set to an instance of an object.

ERROR DELETE System.Web.Http.Controllers WatchlistController ExecuteAsync Object reference not set to an instance of an object.

INFO DELETE System.Net.Http.Formatting DefaultContentNegotiator Negotiate Type='HttpError', formatters=[JsonMediaTypeFormatterTracer, XmlMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer]
INFO DELETE System.Net.Http.Formatting JsonMediaTypeFormatter GetPerRequestFormatterInstance Obtaining formatter of type 'JsonMediaTypeFormatter' for type='HttpError', mediaType='application/json; charset=utf-8'
INFO DELETE System.Net.Http.Formatting JsonMediaTypeFormatter GetPerRequestFormatterInstance Will use same 'JsonMediaTypeFormatter' formatter
INFO DELETE System.Net.Http.Formatting DefaultContentNegotiator Negotiate Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'
INFO DELETE System.Web.Http.Request Content-type='application/json; charset=utf-8', content-length=unknown
INFO DELETE System.Net.Http.Formatting JsonMediaTypeFormatter WriteToStreamAsync Value='System.Web.Http.HttpError', type='HttpError', content-type='application/json; charset=utf-8'
INFO DELETE System.Net.Http.Formatting JsonMediaTypeFormatter WriteToStreamAsync
INFO DELETE System.Web.Http.Controllers WatchlistController Dispose
INFO DELETE System.Web.Http.Controllers WatchlistController Dispose

0 个答案:

没有答案