使用$select
和$expand
等功能将所有包WebApi和OData升级为预发布版本(来自aspnetwebstack)。
Microsoft.AspNet.WebApi - > 5.0.0-beta1-130514
Microsoft.AspNet.WebApi.Client - > 5.0.0-beta1-130514
Microsoft.AspNet.WebApi.Core - > 5.0.0-beta1-130514
Microsoft.AspNet.WebApi.OData - > 5.0.0-beta1-130514
Microsoft.AspNet.WebApi.Web ... - > 5.0.0-beta1-130514
我的API控制器有一个基类:
public class baseApiController<T> : EntitySetController<T, int>
where T: class, IEntity, new()
{
public IRepository Repositorio { get; private set; }
public baseApiController(IRepository repositorio)
{
Repositorio = repositorio;
}
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All, PageSize=20)]
public override IQueryable<T> Get()
{
return Repositorio.Query<T>();
}
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
protected override T GetEntityByKey(int key)
{
return Repositorio.Get<T>(key);
}
}
用户控制器
[Authorize]
public class usuariosController : baseApiController<Usuario>
{
public usuariosController(IRepository repositorio)
: base(repositorio)
{ }
[Authorize(Roles="Admin,TI")]
public HttpResponseMessage post(Usuario usuario)
{
var x = WebSecurity.CreateUserAndAccount(usuario.Email, "maisbb", new { Nome = usuario.Nome }); //TODO: Não fixar senha
Repositorio.Store(usuario);
return Request.CreateResponse(HttpStatusCode.OK, usuario);
}
[HttpGet, Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable roles([FromODataUri] int key)
{
var usuario = (from u in Repositorio.Query<Usuario>()
where u.Id == key
select new { u.Email }).SingleOrDefault();
return Roles.GetRolesForUser(usuario.Email).AsQueryable();
}
}
操作roles
检索特定用户的所有角色:
目标是使用此URL 检索用户的所有角色:
/api/usuarios(67)/roles
我按如下方式配置了我的API WebApiConfig
:
modelBuilder.EntitySet<Usuario>("usuarios");
var entityTypeUsuario = modelBuilder.Entity<Usuario>();
var actRoles = entityTypeUsuario.Action("roles");
actRoles.Parameter<int>("key");
actRoles.Returns<string[]>();
...
var model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute(routeName: "OData", routePrefix: "api", model: model);
config.EnableQuerySupport();
var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
var enumConverter = new StringEnumConverter();
jsonFormatter.SerializerSettings.Converters.Add(enumConverter);
config.Formatters.Remove(config.Formatters.XmlFormatter);
var jqueryFormatter = config.Formatters.FirstOrDefault(x => x.GetType() == typeof(JQueryMvcFormUrlEncodedFormatter));
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
config.Formatters.Remove(jqueryFormatter);
config.Formatters.JsonFormatter.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
获取:/api/usuarios(67)/role
“此服务不支持'〜/ entityset / key / action'形式的OData请求。”
答案 0 :(得分:6)
ODataActions应该始终是POST请求。您是否可以尝试修改以下代码并查看它是否可以解决问题?
[HttpPost, Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable roles([FromODataUri] int key, ODataActionParameters parameters)
{
var usuario = (from u in Repositorio.Query<Usuario>()
where u.Id == key
select new { u.Email }).SingleOrDefault();
return Roles.GetRolesForUser(usuario.Email).AsQueryable();
}
此外,在构建模型时,您需要 not 指定参数'key',因为此密钥是从Uri本身检索的。更新了以下代码:
modelBuilder.EntitySet<Usuario>("usuarios");
var entityTypeUsuario = modelBuilder.Entity<Usuario>();
var actRoles = entityTypeUsuario.Action("roles");
actRoles.Returns<string[]>();
仅供参考:你也可以看看迈克关于OData行动的好帖子:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions