问题描述
问题
当我创建一个具有实际控制上的路线的方法,说:
的API /国家/东西... 的
当我执行上述要求,我的code被执行&安培;数据被返回。
但是当我尝试调用基控制器我的路线。例如:API /国家/ code / 123的
我得到一个404错误。
问
这是如何实现通用的路由,同时利用属性的路由任何想法?
特定的控制器
[路线preFIX(国家)]
公共类CountryController:MasterDataControllerBase<国家,CountryDto>
{
公共CountryController(
IGenericRepository<国家>资源库,
IMappingService映射器,
ISecurityService安全):基地(资料库,映射器,安全)
{
}
}
基本
公共类MasterDataControllerBase< TEntity,TEntityDto> :ControllerBase
其中,TEntity:类,我codedEntity,新的()
其中,TEntityDto:类,新的()
{
私人只读IMappingService映射; 私人只读IGenericRepository< TEntity>库; 私人只读ISecurityService安全; 公共MasterDataControllerBase(IGenericRepository< TEntity>资源库,IMappingService映射器,ISecurityService安全)
{
this.security =安全;
this.mapper =映射;
this.repository =库;
} [路线(code / {code})]
公共TEntityDto获取(字符串code)
{
this.security.Enforce(AccessRight.CanAccessMasterData); VAR的结果= this.repository.FindOne(O =方式>Øcode == code); 返回this.mapper.Map< TEntity,TEntityDto>(结果);
}
}
属性中的Web API路由不支持路线
属性的继承。您可以看到这表明继承= FALSE
如果查看 RouteAttribute
类的定义...
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,继承=假,的AllowMultiple = TRUE)]
公共密封类RouteAttribute:属性,IDirectRouteFactory,IHttpRouteInfoProvider
{...
这解释了为什么你得到404,为有效地消失路线
属性。
由于属性是不可继承和类是密封的,我不知道是否有一种用自带开箱的属性路由基础设施来做到这一点。
更新:在Web API团队成员解释说,这是一个设计决定,它不是继承... http://stackoverflow.com/a/19989344/3199781
The problem
When I create a method with a route on the actual controller, say:"api/country/something"...
When I perform the above request, my code gets executed & data gets returned.
But when I try call my route on the base controller. E.G: "api/country/code/123"
I get a 404 error.
Question
Any idea on how to implement generic routes whilst making use of attribute routing?
Specific Controller
[RoutePrefix("Country")]
public class CountryController : MasterDataControllerBase<Country, CountryDto>
{
public CountryController(
IGenericRepository<Country> repository,
IMappingService mapper,
ISecurityService security) : base(repository, mapper, security)
{
}
}
Base
public class MasterDataControllerBase<TEntity, TEntityDto> : ControllerBase
where TEntity : class, ICodedEntity, new()
where TEntityDto : class, new()
{
private readonly IMappingService mapper;
private readonly IGenericRepository<TEntity> repository;
private readonly ISecurityService security;
public MasterDataControllerBase(IGenericRepository<TEntity> repository, IMappingService mapper, ISecurityService security)
{
this.security = security;
this.mapper = mapper;
this.repository = repository;
}
[Route("code/{code}")]
public TEntityDto Get(string code)
{
this.security.Enforce(AccessRight.CanAccessMasterData);
var result = this.repository.FindOne(o => o.Code == code);
return this.mapper.Map<TEntity, TEntityDto>(result);
}
}
Attribute routing in Web API doesn't support inheritance of the Route
attribute. You can see this indicated by Inherited = false
if you view the definition of the RouteAttribute
class...
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public sealed class RouteAttribute : Attribute, IDirectRouteFactory, IHttpRouteInfoProvider
{ ...
This explains why you're getting the 404, as the Route
attribute effectively disappears.
Since the attribute is not inheritable and the class is sealed, I don't know if there's a way to do this with the attribute routing infrastructure that comes out of the box.
Update: A member of the Web API team explains it was a design decision that it's not inherited... http://stackoverflow.com/a/19989344/3199781
这篇关于网页API。通用控制器属性路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!