0
是否可以向所有控制器添加强制参数? 我开发RESTful api,所以我想为每个路由都要求特殊的“apikey”参数。AttributeRouting强制性参数
[HttpPut]
[PUT("create")]
public PostDto Create(string title, string description, string tag, long photo, float lat, float lon, int error)
{
if (description.Length > DescriptionMaxLength)
throw new ApiException(ErrorList.TooLongDescription, string.Format("Description is more than {0}", DescriptionMaxLength));
throw new NotImplementedException();
}
[HttpPost]
[POST("edit/{id:int}")]
public bool Edit(int id, string title, string description, int? photo)
{
if (description.Length > DescriptionMaxLength)
throw new ApiException(ErrorList.TooLongDescription, string.Format("Description is more than {0}", DescriptionMaxLength));
throw new NotImplementedException();
}
[HttpDelete]
[DELETE("delete/{id:int}")]
public bool Delete(int id)
{
if (id < 0)
throw new ApiException(ErrorList.InvalidValue, "id is smaller than 0");
throw new NotImplementedException();
}
但我不想为每种方法手动执行此操作。
对于像'apikey'这样的必要参数,我将在验证方法中使用BaseController或ActionFilter进行验证。如果您稍后需要访问apikey,则始终可以假定它存在。如果需要,您也可以将过滤器将api键推入Context.Items集合中或类似的东西。 –