简体   繁体   中英

ASP.net client and ODataController issues

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.

I think I may have figured it out and it makes sense. So if you create an OdataController you sorta just have your default Post to create a new item, Patch to change/update an update, Put to replace an item with a new one...

Then if you want custom api calls you need to use actions: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/odata-actions

So I was just doing it all wrong. I just assumed you could put Post in front of some function name and it would be a Post opperation with custom arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM