In my Web App I am trying to update my Album
. To do so, I have an OData Web API. I added the OData Web API service to my Web App using this tutorial.
I am having a problem with calling the DataServiceContext.SaveChanges()
after I called the DataServiceContext.UpdateObject
. This is the error I receive:
I also use DataServiceContext.SaveChanges()
in other actions (Get and Delete), and there everything works fine. I just get this error in my Edit action.
Microsoft.OData.Client.DataServiceRequestException
HResult=0x80131509
Message=An error occurred while processing this request.
Source=Microsoft.OData.Client
StackTrace:
at Microsoft.OData.Client.SaveResult.HandleResponse()
at Microsoft.OData.Client.BaseSaveResult.EndRequest()
at Microsoft.OData.Client.DataServiceContext.SaveChanges(SaveChangesOptions options)
at biz.dfch.CS.Examples.SampleAspNetCoreWebApp.Controllers.AlbumsController.EditAlbum(Album album) in C:\src\biz.dfch.CS.Examples.SampleAspNetCoreWebApp\src\biz.dfch.CS.Examples.SampleAspNetCoreWebApp\Controllers\AlbumsController.cs:line 64
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
Inner Exception 1:
DataServiceClientException: NotFound
This is my Web App Controller:
public class AlbumsController : Controller
{
private Container container;
public AlbumsController()
{
container = new Container(new Uri("https://localhost:44316/odata/"));
}
public IActionResult EditAlbum(Album album)
{
container.AttachTo("Albums", album);
container.UpdateObject(album);
container.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
return RedirectToAction(nameof(Index));
}
}
This is my Web API controller:
public class AlbumsController : ODataController
{
[HttpPut]
public IActionResult UpdateAlbum([FromODataUri] int key,[FromBody] Album album)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != album.Id)
{
return BadRequest();
}
_context.Update(album);
try
{
_context.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!AlbumExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(album);
}
private bool AlbumExists(int key)
{
return _context.Albums.Any(a => a.Id == key);
}
}
This is my view:
@model biz.dfch.CS.Examples.SampleAspNetCoreWebApi.Models.Album
<div class="row">
<div class="col-md-4">
<form asp-action="EditAlbum">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
This is the Album model:
public class Album
{
public Album()
{
Songs = new List<Song>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Song> Songs { get; set; }
}
The exception you got points to the problem in the connection (DataServiceClientException: NotFound). It could mean that the service you told it to connect to is not reachable under a given URL.
Are you 100% certain your service is running on https://localhost:44316/odata/
?
My guess is that it is running on that port, but not under https
.
Try changing the address to: http://localhost:44316/odata/
and see if that works.
User contributions licensed under CC BY-SA 3.0