7

It seems ASP.NET implicitly interprets method named GetX and PostX as GET and POST methods respectively, because of their names are prefixed with HTTP method names. This is also the case for PUT and DELETE.

I have a method unfortunately named Delete but I want it to be interpreted as a POST, so I explicitly specify it's a POST using the [HttpPost] attribute. This works, as long as it's not declared inside an interface...

public interface IFooBarController
{
    [HttpPost]
    void DoSomething();

    [HttpPost]
    void Delete(int id);
}

public class FooBarController : IFooBarController
{
    public void DoSomething()
    {
       // This API method can only be called using POST,
       // as you would expect from the interface.
    }

    public void Delete(int id)
    {
        // This API method can only be called using DELETE,
        // even though the interface specifies [HttpPost].
    }
}

How can I work around this, without having to specify the HttpPostAttribute for each implementation?

5
  • Will you be using Attribute routing with this web api? Commented Mar 10, 2016 at 12:20
  • 1
    It seems to me that http verbs are implementation details and don't belong in an interface. Commented Mar 10, 2016 at 12:39
  • @BigDaddy, I would argue they are a part of the API method signature. Commented Mar 10, 2016 at 12:55
  • @Nkosi, no, we currently have our routing configured to include the full C# method name in the route. Commented Mar 10, 2016 at 12:58
  • 1
    I thought you might say that ;) The class is required to provide the implementation of interface methods. Why not convert the interface to an abstract class? Commented Mar 10, 2016 at 13:05

4 Answers 4

4

Attributes on interface properties doesn't get inherited to the class, you may make your interface to an Abstract Class.

Found an answer from Microsoft:

The product team does not want to implement this feature, for two main reasons:

  • Consistency with DataAnnotations.Validator
  • Consistency with validation behavior in ASP.Net MVC
  • tricky scenario: a class implements two interfaces that have the same property, but with conflicting attributes on them. Which attribute would take precedence?

SOURCES: Attribute on Interface members does not work

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

3 Comments

I asked this on another question but I'll repeat it here. If attributes aren't inherited, how come an interface method [HttpPost] DoSomething() can only be called using POST, even though the implementation of said interface does not repeat the HttpPostAttribute?
you have to bypass an abstract class.
wait i can t verify now ;) i ll do it when i m back to home :p
2

Like others have said, attributes are not inherited. DoSomething is not called using POST because of the attribute in your interface but because that is the default. Change it to GET in your interface and you will still notice POST calling it.

You can read more on how action selection is performed here in the section "Action Selection". (Item 3 answers your question regarding why DoSomething is called using POST)

Comments

1

Attributes aren't inherited from implemented interfaces, you'll have to declare the attribute on the concrete implementation (FooBarController). This overrides the convention-based binding.

1 Comment

If that's true, how come an interface method [HttpPost] DoSomething() can only be called using POST, even though the implementation of said interface does not repeat the HttpPostAttribute?
-2

use [ActionName] attribue and name it as you want

Comments

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.