BHuelse BHuelse - 1 month ago 23
C# Question

How to intercept filterchain for maintenance in a WebApi project

I have a

WebApi
project, where I want to implement a maintenancefilter.
Now, I have the problem, that the filter is called twice. So I got the correct http statuscode, but the filter don't intercept and my methods in the controlled are called, normally.

What I have to do, that my filter intercepts correctly and no other method is called?

public class MaintenanceFilter : ActionFilterAttribute
{
[Dependency]
public IUaCRepository UaC { get; set; }

public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
}

public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (UaC != null && UaC.CheckMaintenance(WebApiConfig.CONFIG_STANDARD))
{
var response = actionExecutedContext.Response;
response.StatusCode = HttpStatusCode.ServiceUnavailable;

return;
}
base.OnActionExecuted(actionExecutedContext);
}
}


best regards

[EDIT] This soleved my problem:

public override void OnActionExecuting(HttpActionContext actionContext)
{
if (UaC != null && UaC.CheckMaintenance(WebApiConfig.CONFIG_STANDARD))
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, "Maintenance");

return;
}
base.OnActionExecuting(actionContext);
}

Answer

Override the OnActionExecuting method providing the fixed response. In this way it will not go on to with the request processing