我有一个 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