NullReferenceException - web api错误。 [英] NullReferenceException -- web api error.

查看:88
本文介绍了NullReferenceException - web api错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是能够从一个控制器调用GET和POST请求,如果可能的话。使用私有方法,将允许我从URI传递GET的输入参数,并从正文传递POST。但是,在实现逻辑时,我不确定采取什么方法。



当我在Fiddler中调用POST请求时,我当前收到错误 - > ; NullReferenceException - 错误 - >在私有方法调用内,对象引用未设置为对象的实例。私有方法请求适用于GET URI请求但不适用于POST请求。



My goal is to be able to call both GET and POST request from one controller, if possible. Having a private method, will allow me to pass input parameters for GET from the URI and for POST from the body. However, I am little unsure of what approach to take, in implementing the logic.

I am currently receiving an error, when calling the POST request in Fiddler --> NullReferenceException -- error --> Object reference not set to an instance of an object, inside private method call. The private method request works fine for GET URI request but not for POST request.

public static HttpContext Current { get; set; }

         [HttpGet]
         public HttpResponseMessage get([FromUri] Query query)
         {
             return method(Current, query);
         }

         [HttpPost]
         public HttpResponseMessage post([FromBody] Query query)
         {
             return method(Current, query);
         }

         private HttpResponseMessage method(HttpContext request, Query query)
         {
           //if (query == null)
             // return Request.CreateResponse(HttpStatusCode.BadRequest, "query null");

                if (User.IsInRole("admin"))
             {

                 IQueryable<data> Data = null;

                 //error line below
                 if (!string.IsNullOrEmpty(query.name))
                 {
                     var ids = query.name.Split(',');

                     var dataMatchingTags = db.data.Where(c => ids.Any(id => c.Name.Contains(id)));

                     if (Data == null)
                         Data = dataMatchingTags;
                     else
                         Data = Data.Union(dataMatchingTags);
                 }

                 if (Data == null)
                     Data = db.data;

                 if (query.startDate != null)
                 {
                     Data = Data.Where(c => c.UploadDate >= query.startDate);
                 }

                 Data = Data.OrderByDescending(c => c.UploadDate);

                 var data = Data.ToList();

                 if (!data.Any())
                 {
                     var message = string.Format("No data found");
                     return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
                 }

                 return Request.CreateResponse(HttpStatusCode.OK, data);
             }

             return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Access Denied, Please try again.");
         }





我已将响应代码添加到我的私有方法中,以测试Fiddler中的调用并回复结果如下:





I have added in response code to my private method, to test the call in Fiddler and it responded back with the following result:

HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcbWlzc3lcRG9jdW1lbnRzXFZpc3VhbCBTdHVkaW8gMjAxMlxQcm9qZWN0c1xBUElfMjAxNFxUaGVhdGljYV9KdW5lMjAxNFxWM190ZXN0aW5nXEFQSV8xN09jdFxBUElfMDlPY3RcYXBpXHN1YnNldA==?=
X-Powered-By: ASP.NET
Date: Thu, 31 Jul 2014 09:40:16 GMT
Content-Length: 12

"query null"





Fiddler - 测试POST请求:

在Fiddler的作曲家标签中,我复制在URL中 - [http:// localhost:45361 / api / subset]。在请求标题框中:



Fiddler - Testing POST Request:
In the composer tab of the Fiddler, I copy in the URL -- [http://localhost:45361/api/subset]. In the Request header box:

User-Agent: Fiddler<br />
Content-Type: application/json;<br />
Host: localhost:45361<br />
Content-Length: 16<br />
Authorization: Basic ###########==<br />



在Request Body框中:

{name:store}



如何在发出POST请求时阻止此操作,如何获取输出数据的POST请求,与GET请求相同。



请帮助和建议。



非常感谢你的时间和帮助。


In the Request Body box:
{"name":"store"}

How can I prevent this while making a POST request and How can I get my POST request to output data, as same as GET request.

Please help and advice.

Many thanks for your time and help.

推荐答案

这篇关于NullReferenceException - web api错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆