【发布时间】:2017-08-07 16:09:05
【问题描述】:
如果我有控制器 GET 方法,例如:
[HttpGet]
[Route("accountrecv({accountid})/promisepay", Name = "GetPromisePay")]
public HttpResponseMessage GetPromisePay(int accountid)
{
var query = Request.RequestUri.Query;
var uri = new Uri(Client.Instance.BaseAddress.ToString() + accountid + "/promisepay" + query);
var request = new HttpRequestMessage { RequestUri = uri, Method = HttpMethod.Get };
var response = Client.Instance.SendAsync(request);
return response.Result;
}
如何在HttpStatusCode 不正确的所有响应上施加行为?
我想我会在方法级别创建一个属性,例如:
[NonOKResponse]
[HttpGet]
[Route.......]
public HttpResponseeMessage GetPromisePay(int accountid)
{
//my code
return response.Result //but force it here to return 404 for EVERY response other than 200
}
我如何定义一个可以在所有 GET 上使用的属性,以根据某些标准强制执行特定响应?
【问题讨论】:
-
为什么不在返回结果前检查
response的状态码? -
因为我有数百个控制器,可能还有数千个方法,我需要应用这种行为
-
实现
IResultFilter的ActionFilter 可以在这里工作吗?
标签: c# asp.net .net asp.net-web-api2 visual-studio-2017