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集合中或類似的東西。 –