Reputation: 320
I'm using ASP.NET WEB API 2 to migrate an existing web service.
Below is set of filter that I use
public class ValidateSession : ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
//var requestMessage = actionContext.Request.Content.ToString();
if (!actionContext.Request.Headers.Contains("source"))
{
actionContext.Request.Headers.Add("source", "1");
}
if (!actionContext.Request.Headers.Contains("appstore_session_id"))
{
actionContext.Response = actionContext.Request.CreateErrorResponse(System.Net.HttpStatusCode.BadRequest, "Session id is not included in the header");
}
}
}
Below is the code for override the previous ActionFilter attribute
public class OverrideSessionValidation : ActionFilterAttribute, IOverrideFilter
{
public Type FiltersToOverride
{
get { return typeof(ValidateSession); }
}
public bool AllowMultiple
{
get { return true; }
}
}
The code for controller is also give below
[ValidateSession]
public class SampleController : ApiController
{
public string GetSessionValues()
{
return "from session vals";
}
[OverrideSessionValidation]
public string GetDefaultVals()
{
return "from DefVals";
}
}
It can be seen that I have placed the validate session at the class level and for one method I want to override the same. Hence for the second method I used overrideSessionValidation. Though the
FiltersToOverride of OverrideSessionValidation is called, I see that onActionExcuting for ValidateSession is also called. I expect that the class filter onActionExecuting should not be called as I have Override for the same.
Please let me know what is the error so that I can solve this problem
Thanks and regards Venkatesh
Upvotes: 2
Views: 2079
Reputation: 320
After much research and looking into the Native code of ASP.NET MVC, I realize that we cannot override a single action attribute. Hence I used delegate Message handler and if action attributes are needed then use them at action level and not at Class level
Upvotes: 1