From 01527cd4df639723dd06d6b64aff575ec0b80e71 Mon Sep 17 00:00:00 2001 From: harshgMSFT Date: Mon, 7 Apr 2014 15:42:10 -0700 Subject: [PATCH] Adding HttpPost, HttpDelete, HttpPut, HttpPatch attributes --- .../HttpDeleteAttribute.cs | 16 ++++++++ .../HttpPatchAttribute.cs | 16 ++++++++ .../HttpPostAttribute.cs | 16 ++++++++ .../HttpPutAttribute.cs | 16 ++++++++ .../ActionAttributeTests.cs | 38 ++++++++++++++++--- 5 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Core/HttpDeleteAttribute.cs create mode 100644 src/Microsoft.AspNet.Mvc.Core/HttpPatchAttribute.cs create mode 100644 src/Microsoft.AspNet.Mvc.Core/HttpPostAttribute.cs create mode 100644 src/Microsoft.AspNet.Mvc.Core/HttpPutAttribute.cs diff --git a/src/Microsoft.AspNet.Mvc.Core/HttpDeleteAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/HttpDeleteAttribute.cs new file mode 100644 index 0000000000..e9f77b94ff --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/HttpDeleteAttribute.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; + +namespace Microsoft.AspNet.Mvc +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public sealed class HttpDeleteAttribute : Attribute, IActionHttpMethodProvider + { + private static readonly IEnumerable _supportedMethods = new string[] { "DELETE" }; + + public IEnumerable HttpMethods + { + get { return _supportedMethods; } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/HttpPatchAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/HttpPatchAttribute.cs new file mode 100644 index 0000000000..f5df3778a2 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/HttpPatchAttribute.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; + +namespace Microsoft.AspNet.Mvc +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public sealed class HttpPatchAttribute : Attribute, IActionHttpMethodProvider + { + private static readonly IEnumerable _supportedMethods = new string[] { "PATCH" }; + + public IEnumerable HttpMethods + { + get { return _supportedMethods; } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/HttpPostAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/HttpPostAttribute.cs new file mode 100644 index 0000000000..e959b90bef --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/HttpPostAttribute.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; + +namespace Microsoft.AspNet.Mvc +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public sealed class HttpPostAttribute : Attribute, IActionHttpMethodProvider + { + private static readonly IEnumerable _supportedMethods = new string[] { "POST" }; + + public IEnumerable HttpMethods + { + get { return _supportedMethods; } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/HttpPutAttribute.cs b/src/Microsoft.AspNet.Mvc.Core/HttpPutAttribute.cs new file mode 100644 index 0000000000..ac24396344 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/HttpPutAttribute.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; + +namespace Microsoft.AspNet.Mvc +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public sealed class HttpPutAttribute : Attribute, IActionHttpMethodProvider + { + private static readonly IEnumerable _supportedMethods = new string[] { "PUT" }; + + public IEnumerable HttpMethods + { + get { return _supportedMethods; } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionAttributeTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionAttributeTests.cs index d23cc7312c..268f0acb9c 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionAttributeTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionAttributeTests.cs @@ -22,7 +22,33 @@ namespace Microsoft.AspNet.Mvc.Core.Test [InlineData("GET")] [InlineData("PUT")] [InlineData("POST")] - public async Task HttpMethodAttribute_ActionDecoratedWithMultipleHttpMethodAttribute_ORsMultipleHttpMethods(string verb) + [InlineData("DELETE")] + [InlineData("PATCH")] + public async Task HttpMethodAttribute_ActionWithMultipleHttpMethodAttributeViaAcceptVerbs_ORsMultipleHttpMethods(string verb) + { + // Arrange + var requestContext = new RequestContext( + GetHttpContext(verb), + new Dictionary + { + { "controller", "HttpMethodAttributeTests_RestOnly" }, + { "action", "Patch" } + }); + + // Act + var result = await InvokeActionSelector(requestContext); + + // Assert + Assert.Equal("Patch", result.Name); + } + + [Theory] + [InlineData("GET")] + [InlineData("PUT")] + [InlineData("POST")] + [InlineData("DELETE")] + [InlineData("PATCH")] + public async Task HttpMethodAttribute_ActionWithMultipleHttpMethodAttributes_ORsMultipleHttpMethods(string verb) { // Arrange var requestContext = new RequestContext( @@ -155,15 +181,15 @@ namespace Microsoft.AspNet.Mvc.Core.Test private class HttpMethodAttributeTests_RestOnlyController { [HttpGet] - [AcceptVerbs("PUT", "POST")] + [HttpPut] + [HttpPost] + [HttpDelete] + [HttpPatch] public void Put() { } - public void Delete() - { - } - + [AcceptVerbs("PUT", "post", "GET", "delete", "pATcH")] public void Patch() { }