Asp.net web api ASP.NET Web API为非GET请求提供空引用错误
我有一个ApiController,它可以接收和响应HTTP Get请求,但当我尝试使用单个Guid或字符串参数发布或删除任何内容时,我会从框架中得到一个空引用异常 令人沮丧的是,这可以在我的本地机器上运行,但是当我在服务器上运行它时,就会出现这个错误 我在这里做错了什么吗: 控制器代码:Asp.net web api ASP.NET Web API为非GET请求提供空引用错误,asp.net-web-api,Asp.net Web Api,我有一个ApiController,它可以接收和响应HTTP Get请求,但当我尝试使用单个Guid或字符串参数发布或删除任何内容时,我会从框架中得到一个空引用异常 令人沮丧的是,这可以在我的本地机器上运行,但是当我在服务器上运行它时,就会出现这个错误 我在这里做错了什么吗: 控制器代码: <Authorize()> Public Class WatchlistController Inherits ApiController Private ReadOnly watc
<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
上面的Test()和TestReturn()方法可以工作,但是AddItem和removItem不工作。
日志表明已正确找到操作和参数,但随后引发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
用户或用户。标识可以为null?是的,看起来是这样。很明显-有第二双眼睛很好!
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