通过Azure App Service过滤OData

时间:2018-01-25 06:46:02

标签: c# asp.net web-services azure

我创建WebService以使用Azure App Service调用OData,我能够通过Postman从OData获取数据。我可以用硬编码网址过滤数据。但现在我想过滤以通过我的webservice获取特定数据。这里的示例是我的OData Url public static string sessionUrl1 = "/api/data/v9.0/contacts,如果我想添加过滤器,我只会在网址中添加过滤器,例如sessionUrl1 = "/api/data/v9.0/contacts?$filter=fullname eq 'Ken Arok'";,但我知道我想通过我的网络服务过滤它,例如我的webservice是{ {1}},我希望通过我的webservice网址过滤它http://aloha.azurewebsites.net/如何实现这一点,我应该在Web服务中添加Post方法吗?我已经创建了Post方法,但它仍然可以工作,也许你有消化我应该在这里添加什么?

http://customroutes.azurewebsites.net/User?$filter=fullname eq 'Ken Arok'"

我的模特

 [HttpPost]
        [Route("FilterContact")]

        public string GetFilter([FromBody]FilterModel filter)
        {
            filter.Filter = null;
            string GetUserSessionOperationPath = string.Format("{0}{1}", ClientConfiguration.Default.UriString.TrimEnd('/'), sessionUrl + filter.Filter);

            try
            {
                // Creates an HttpWebRequest for user session URL.
                HttpWebRequest aadRequest = (HttpWebRequest)WebRequest.Create(GetUserSessionOperationPath);

                // Change TLS version of HTTP request if the TLS version value is defined in ClientConfiguration
                if (!string.IsNullOrWhiteSpace(ClientConfiguration.OneBox.TLSVersion))
                {
                    aadRequest.ProtocolVersion = Version.Parse(ClientConfiguration.OneBox.TLSVersion);
                }

                string tlsRequestVersion = aadRequest.ProtocolVersion.ToString();
                Console.WriteLine("The TLS protocol version for the HTTP request is {0}.", tlsRequestVersion);

                aadRequest.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader();
                aadRequest.Method = "GET";
                aadRequest.ContentLength = 0;

                // Get HttpWebResponse for the response
                var aadResponse = (HttpWebResponse)aadRequest.GetResponse();

                string tlsResponseVersion = aadResponse.ProtocolVersion.ToString();
                Console.WriteLine("The TLS protocol version of the server response is {0}.", tlsResponseVersion);

                if (aadResponse.StatusCode != HttpStatusCode.OK)
                {
                    Console.WriteLine("Could not get response from the server.");
                    //return;
                }

                // Get response string
                using (Stream responseStream = aadResponse.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        string responseString = streamReader.ReadToEnd();

                        Console.WriteLine(string.Format("\nSuccessfully received response.\nResponse string: {0}.", responseString));
                        aadResponse.Close();
                        return responseString;
                    }
                }


                // Releases the resources of the response.

            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with the exception: {0} and stack trace: {1}.", ex.ToString(), ex.StackTrace);
                throw new Exception(ex.Message);
            }

            //  Console.ReadLine();
        }

1 个答案:

答案 0 :(得分:1)

根据您的要求,我刚刚创建了我的Web API项目来测试此问题。您可以按照以下方法实现您的目的。

public class ValuesController : ApiController
{
    // GET http://localhost/api/values?$expand=Supplier&$filter=fullname eq 'Ken Arok'
    public IHttpActionResult Get()
    {
        var dics = Request.GetQueryNameValuePairs().ToDictionary(q=>q.Key,q=>q.Value);
        string filter = null;
        dics.TryGetValue("$filter", out filter);

        //TODO:
        return Content(HttpStatusCode.OK,filter);
        //return Json(Request.GetQueryNameValuePairs().Select(q => new { key = q.Key, value = q.Value }));
    }
}

或者

<强>型号:

public class FilterModel
{
    [JsonProperty("$filter")]
    public string Filter { get; set; }

    [JsonProperty("$expand")]
    public string Expand { get; set; }

    //add other options for OData
}

public class ValuesController : ApiController
{
    // POST api/values
    public IHttpActionResult Post([FromBody]FilterModel filter)
    {   
        //TODO:
        return Json(filter);
    }
}

<强> TEST:

enter image description here