2

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

2
  • 1
    Possibly you are overriding wrong action attribute. I suspect you override the one from System.Web.Mvc but the correct one is in System.Web.Http.Filters. Commented Dec 30, 2013 at 11:39
  • No Since I have created ASP.NET MVC 2 EMPTY WEB API project I do not have references of MVC at all. I'm 100% Sure that I'm using only System.Web.Http.Filters Commented Dec 30, 2013 at 12:44

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I've just tried something similar. This makes action filter overrides completely useless to me as I have lots of global action filters and only wish to override one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.