1197 Support [ResponseType(typeof(void))] for actions that return an IHttpActionResult with no response content

This commit is contained in:
yaohuang 2013-09-10 14:19:37 -07:00
Родитель b8a4ea9ae7
Коммит 532db0188d
3 изменённых файлов: 13 добавлений и 5 удалений

Просмотреть файл

@ -362,7 +362,7 @@ namespace System.Web.Http.Description
// response formatters
ResponseDescription responseDescription = CreateResponseDescription(actionDescriptor);
Type returnType = responseDescription.ResponseType ?? responseDescription.DeclaredType;
IEnumerable<MediaTypeFormatter> supportedResponseFormatters = returnType != null ?
IEnumerable<MediaTypeFormatter> supportedResponseFormatters = (returnType != null && returnType != typeof(void)) ?
actionDescriptor.Configuration.Formatters.Where(f => f.CanWriteType(returnType)) :
Enumerable.Empty<MediaTypeFormatter>();

Просмотреть файл

@ -24,5 +24,11 @@ namespace System.Web.Http.ApiExplorer
{
return "User deleted";
}
[ResponseType(typeof(void))]
public IHttpActionResult Head()
{
return StatusCode(HttpStatusCode.OK);
}
}
}

Просмотреть файл

@ -12,10 +12,11 @@ namespace System.Web.Http.ApiExplorer
public class ResponseTypeAttributeTest
{
[Theory]
[InlineData("Get", typeof(IHttpActionResult), typeof(User))]
[InlineData("Post", typeof(HttpResponseMessage), typeof(User))]
[InlineData("Delete", typeof(string), null)]
public void DeclaredResponseType_AppearsOnApiDescription(string actionName, Type declaredType, Type responseType)
[InlineData("Get", typeof(IHttpActionResult), typeof(User), 2)]
[InlineData("Post", typeof(HttpResponseMessage), typeof(User), 2)]
[InlineData("Delete", typeof(string), null, 2)]
[InlineData("Head", typeof(IHttpActionResult), typeof(void), 0)]
public void DeclaredResponseType_AppearsOnApiDescription(string actionName, Type declaredType, Type responseType, int responseFormattersCount)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });
@ -29,6 +30,7 @@ namespace System.Web.Http.ApiExplorer
api.ResponseDescription.ResponseType == responseType);
Assert.NotNull(expectedApi);
Assert.Equal(responseFormattersCount, expectedApi.SupportedResponseFormatters.Count);
}
}
}