Reputation: 1486
I Create WebService to call OData with Azure App Service, Im able to get the data from OData via Postman. Im Able to Filter the data with hardcode the url. But now i want to aply filter to get specific data through my webservice. Example here is my OData Url public static string sessionUrl1 = "/api/data/v9.0/contacts
, if i want to add filter i will just add the filter in url like this sessionUrl1 = "/api/data/v9.0/contacts?$filter=fullname eq 'Ken Arok'";
but know i want to filter it through my web service, example my webservice is http://aloha.azurewebsites.net/
, and i want to filter it through my webservice url like this http://customroutes.azurewebsites.net/User?$filter=fullname eq 'Ken Arok'"
How to Achieve this , should i Add Post Method In Web Service ? i already create Post method but its still does'nt work maybe you Have sugestion What should i add here ?
[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();
}
my Model
public class FilterModel
{
public string Filter { get; set; }
}
Upvotes: 0
Views: 326
Reputation: 18465
According to your requirement, I just created my Web API project to test this issue. You could follow the approaches below to achieve your purpose.
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 }));
}
}
Or
Model:
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:
Upvotes: 1