作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 WEB API ODATA 与 Telerik OpenAccess 一起使用
这是 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);
}
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;
}
...
}
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);
}
}
http://localhost//MyERP.Web/odata/NumberSequences(guid'f640510c-365e-434f-9377-0118f22319fc')
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
<?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>
{
"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":""
}
}
}
最佳答案
请将 PutNumberSequence 的“id”参数重命名为“key”。
从
public HttpResponseMessage PutNumberSequence(
[FromODataUri] Guid id, MyERP.DataAccess.NumberSequence entity)
public HttpResponseMessage PutNumberSequence(
[FromODataUri] Guid key, MyERP.DataAccess.NumberSequence entity)
关于odata - WEB API ODATA 错误 "No HTTP resource was found that matches the request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23910710/
我是一名优秀的程序员,十分优秀!