I have an ODataController with 2 methods:
[EnableQuery]
public SingleResult<item> Getitem([FromODataUri] System.Guid key)
{
return SingleResult.Create(db.items.Where(item=> item.guid == key));
}
public async Task<IHttpActionResult> Postitem([FromODataUri] System.Guid key, [FromBody] double itemId)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//Find the correct account
item i = await db.items.FirstAsync(item=> item.guid == key);
if (i == null)
{
return NotFound();
}
//Update the account field we are using for id
i.itemId = itemId;
//Save the changes
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!itemExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(i);
}
private bool itemExists(System.Guid key)
{
return db.items.Count(e => e.guid == key) > 0;
}
With the standard WebApiConfig:
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
config.EnableCors();
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<item>("items");
config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
And I can type mydomain.com/odata/items(guid'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX') in the url bar and get the database object as json just fine.
But when I try the following client code:
var itemGuid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
var itemId = "55555";
using (WebClient wc = new WebClient())
{
wc.UseDefaultCredentials = true;
var domain = "mydomain.com";
var url = domain + "/odata/items(guid'" + itemGuid + "')";
var data = new NameValueCollection();
data["itemId"] = itemId;
byte[] temp = wc.UploadValues(url, "POST", data);
context.Response.Write(Encoding.UTF8.GetString(temp));
}
I get The remote server returned an error: (404) Not Found.
I know it's probably some simple mistake, but been messing around with it too long and I'm new to asp.net.