I am using WEB API ODATA with Telerik OpenAccess
Here is OpenAccessBaseApiController.cs
public abstract partial class OpenAccessBaseApiController<TEntity, TContext> : ODataController
where TContext : OpenAccessContext, new()
{
protected IOpenAccessBaseRepository<TEntity, TContext> repository;
public virtual IQueryable<TEntity> Get()
{
var allEntities = repository.GetAll();
return allEntities;
}
/// <summary>
/// Creates a new entity based on the provided data
/// </summary>
/// <param name="entity">The new entity to be created</param>
/// <returns>HTTP Status:
/// - Accepted when operation is successful or
/// - MethodNotAllowed if the operation is disabled for this entity or
/// - BadRequest if the provided entity is NULL</returns>
public virtual HttpResponseMessage Post(TEntity entity)
{
if (entity == null)
throw new HttpResponseException(HttpStatusCode.BadRequest);
//TODO: should we check if the incomming entity is not an existing one?
TEntity newEntity = repository.AddNew(entity);
var response = CreateResponse(HttpStatusCode.Accepted, newEntity);
return response;
}
protected abstract HttpResponseMessage CreateResponse(HttpStatusCode httpStatusCode, TEntity entityToEmbed);
}
Here is default NumberSequencesController.cs
public partial class NumberSequencesController : OpenAccessBaseApiController<MyERP.DataAccess.NumberSequence, MyERP.DataAccess.EntitiesModel>
{
/// <summary>
/// Constructor used by the Web API infrastructure.
/// </summary>
public NumberSequencesController()
{
this.repository = new NumberSequenceRepository();
}
/// <summary>
/// Dependency Injection ready constructor.
/// Usable also for unit testing.
/// </summary>
/// <remarks>Web API Infrastructure will ALWAYS use the default constructor!</remarks>
/// <param name="repository">Repository instance of the specific type</param>
public NumberSequencesController(IOpenAccessBaseRepository<MyERP.DataAccess.NumberSequence , MyERP.DataAccess.EntitiesModel> repository)
{
this.repository = repository;
}
...
}
Here is my implement NumberSequencesController.partial.cs
public partial class NumberSequencesController
{
public SingleResult<NumberSequence> GetNumberSequence([FromODataUri] Guid key)
{
return SingleResult.Create(repository.GetAll().Where(c => c.Id == key).AsQueryable());
}
/// <summary>
/// Updates single entity.
/// </summary>
/// <remarks>Replaces the whole existing entity with the provided one</remarks>
/// <param name="id">ID of the entity to update</param>
/// <param name="entity">Entity with the new updated values</param>
/// <returns>HttpStatusCode.BadRequest if ID parameter does not match the ID value of the entity,
/// or HttpStatusCode.NoContent if the operation was successful</returns>
public HttpResponseMessage PutNumberSequence([FromODataUri] Guid id, MyERP.DataAccess.NumberSequence entity)
{
if (entity == null || id != entity.Id)
throw new HttpResponseException(HttpStatusCode.BadRequest);
repository.Update(entity);
return Request.CreateResponse(HttpStatusCode.NoContent);
}
}
My ODATA url :
http://localhost//MyERP.Web/odata/NumberSequences(guid'f640510c-365e-434f-9377-0118f22319fc')
work well. But when i PUT update ODATA i have error No HTTP resource was found that matches the request. Here is Fidder data
PUT /MyERP.Web/odata/NumberSequences(guid'f640510c-365e-434f-9377-0118f22319fc')
HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 1855
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
Authorization: Basic REVNTzpXQVpOODFQQy9RY0NsMmRDc01ZZGp3PT0=
Content-Type: text/plain;charset=UTF-8
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,vi;q=0.6
BODY
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<id>http://localhost/MyERP.Web/odata/NumberSequences(guid'f640510c-365e-434f-9377-0118f22319fc')</id>
<category term="MyERP.DataAccess.NumberSequence" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<title />
<updated>2014-05-28T07:50:39Z</updated>
<author><name /></author>
<content type="application/xml">
<m:properties>
<d:ClientId m:type="Edm.Guid">28cc612c-807d-458d-91e7-f759080b0e40</d:ClientId>
<d:Code>GL002</d:Code>
<d:CurrentNo m:type="Edm.Int32">1</d:CurrentNo>
<d:EndingNo m:type="Edm.Int32">9999</d:EndingNo>
<d:FormatNo>GL000</d:FormatNo>
<d:Id m:type="Edm.Guid">f640510c-365e-434f-9377-0118f22319fc</d:Id>
<d:IsDefault m:type="Edm.Boolean">false</d:IsDefault>
<d:Manual m:type="Edm.Boolean">false</d:Manual>
<d:Name>Chung tu tong hop</d:Name>
<d:NoSeqName>seq_no_series_f640510c_365e_434f_9377_0118f22319fc</d:NoSeqName>
<d:OrganizationId m:type="Edm.Guid">4336fecf-8c21-4531-afe6-76d34603ea34</d:OrganizationId>
<d:RecCreated m:type="Edm.DateTime">2014-05-11T00:28:57.754334</d:RecCreated>
<d:RecCreatedBy m:type="Edm.Guid">5e6af2aa-e21a-4afd-815e-0cc3dbefa08a</d:RecCreatedBy>
<d:RecModified m:type="Edm.DateTime">2014-05-11T00:28:57.754334</d:RecModified>
<d:RecModifiedBy m:type="Edm.Guid">5e6af2aa-e21a-4afd-815e-0cc3dbefa08a</d:RecModifiedBy>
<d:StartingNo m:type="Edm.Int32">1</d:StartingNo>
<d:Status m:type="Edm.Int16">1</d:Status>
<d:StatusType>Active</d:StatusType>
<d:Version m:type="Edm.Int64">1</d:Version>
</m:properties>
</content>
It have error :
{
"odata.error":{
"code":"","message":{
"lang":"en-US","value":"No HTTP resource was found that matches the request URI 'http://localhost/MyERP.Web/odata/NumberSequences(guid'f640510c-365e-434f-9377-0118f22319fc')'."
},"innererror":{
"message":"No action was found on the controller 'NumberSequences' that matches the request.","type":"","stacktrace":""
}
}
}
Please rename the "id" parameter of PutNumberSequence to "key".
From
public HttpResponseMessage PutNumberSequence(
[FromODataUri] Guid id, MyERP.DataAccess.NumberSequence entity)
To
public HttpResponseMessage PutNumberSequence(
[FromODataUri] Guid key, MyERP.DataAccess.NumberSequence entity)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With