3

I'm using a custom filter to validate the content type, like:

        public override void OnActionExecuting(HttpActionContext httpActionContext)
        {
            List<String> errors = new List<String>();

            // a
            if (httpActionContext.Request.Content.Headers.ContentType.MediaType == "application/json")
            {
            }
            else
            {
                errors.Add("Invalid content type.");
            }

                // more checks
        }

The above code is working fine, but the validation should check the request http verb, because it should validate the content type only for put or post. I don't want to remove the custom filter from httpget actions because I have more checks inside it, and I don't want to split the filter in two parts, meaning I have to check the http verb inside the filter, but I can't find how.

Any tips?

5
  • Why are you trying to restrict the media representation of the data? Out of the box both XML and JSON are supported, and the media handlers convert the representation to a strongly typed object for you. It would probably be better to have you API communicate the media types it accepts and then return a Bad Request error if necessary. Commented Aug 14, 2012 at 15:12
  • Because we will only support json (documentation, versioning, etc). Commented Aug 14, 2012 at 15:19
  • I guess that is up to you, but the ability to support multiple media types and letting the web API handle the format to object translation isn't something I would discard lightly. Commented Aug 14, 2012 at 16:02
  • @Oppositional only a quick comment to say you would probably return a 415 Unsupported Media Type not bad request. Commented Aug 14, 2012 at 20:07
  • @Mark Jones, you are correct that was what I intended to say ;-) Good catch! Commented Aug 14, 2012 at 21:13

4 Answers 4

8

You can get the method type (post or put) from this:

public override void OnActionExecuting(HttpActionContext actionContext)
{
    string methodType = actionContext.Request.Method.Method;
    if (methodType.ToUpper().Equals("POST") 
            || methodType.ToUpper().Equals("PUT"))
    {
         // Your errors
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It's a commercial api, and we will only provide support and documentation for json. We will not support anything related to xml. It's a business requirement.
@user1330271 If you "will not support anything related to xml" then to fully remove XML you can remove the XML format in the config: void ConfigureApi(HttpConfiguration config) { // Remove the XML formatter config.Formatters.Remove(config.Formatters.XmlFormatter); }
#Facepalm why didn't WebAPI ship with HttpVerbs that is available in MVC? I am glad they're merging the two in ASP.NET 5
@CuongLe how i can get posted values of action (in object not in querystring). in action filter.
3

If you need to get the HTTP Method of the request being validated by the filter, you can inspect the Method property of the request:

var method = actionContext.Request.Method;

I would recommend however that you break the filter apart, as you are quickly headed towards a big ball of mud scenario.

Comments

0

You really should be using the standard HTTPVerb attributes above your controller methods:

[HttpGet]
[HttpPut]
[HttpPost]
[HttpDelete]
[HttpPatch]

MVC Controllers for multiple:

[AcceptVerbs(HttpVerbs.Get, HttpVerbs.Post)]

WebAPI Controlelrs for multiple

[AcceptVerbsAttribute("GET", "POST")]

Comments

-1

In the constructor of the action filter, you can pass in options/named parameters that will set the settings for the OnActionExecuting logic. Based on those settings you can switch up your logic.

public class MyActionFilterAttribute : ActionFilterAttribute
{
    private HttpVerbs mOnVerbs;

    public MyActionFilterAttribute(HttpVerbs onVerbs)
    {
        mOnVerbs = onVerbs;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var currentVerb = filterContext.HttpContext.Request.HttpMethod;

        if (mOnVerbs.HasFlag(HttpVerbs.Post)) { }
        else if (mOnVerbs.HasFlag(HttpVerbs.Get)) { }
        base.OnActionExecuting(filterContext);
    }
}

[MyActionFilter(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Index()
{
}

1 Comment

You're answer is for MVC and not WebAPI

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.