C# 在WebApi2+;中使用AuthorizeAttribute时,HEAD请求失败,System.Net.ProtocolViolationException异常;奥温
我正在开发一个Restful API,在发出实际的删除请求之前,我需要检查授权。所以我认为使用HEAD方法作为飞行前检查会很好 我期望一个未经授权的HEAD方法请求将返回401/403响应,而不包含任何主体。但是,WebApi2崩溃时出现以下异常并关闭连接:C# 在WebApi2+;中使用AuthorizeAttribute时,HEAD请求失败,System.Net.ProtocolViolationException异常;奥温,c#,rest,asp.net-web-api,asp.net-web-api2,owin,C#,Rest,Asp.net Web Api,Asp.net Web Api2,Owin,我正在开发一个Restful API,在发出实际的删除请求之前,我需要检查授权。所以我认为使用HEAD方法作为飞行前检查会很好 我期望一个未经授权的HEAD方法请求将返回401/403响应,而不包含任何主体。但是,WebApi2崩溃时出现以下异常并关闭连接: Exception caught: 'System.Net.ProtocolViolationException' in System.dll ("Bytes to be written to the stream exceed the C
Exception caught: 'System.Net.ProtocolViolationException' in System.dll ("Bytes to be written to the stream exceed the Content-Length bytes size specified.")
其他HTTP方法(DELETE、GET)似乎工作正常-返回401响应
我可以通过GET请求来解决这个问题,但它似乎是WebApi中的一个bug。始终添加内容,无论原始请求方法是什么
我的问题是:这是一个bug还是一个预期的行为,我不应该在这里使用HEAD方法是有原因的
下面是一个复制该问题的示例程序:
namespace OwinServer
{
[Authorize]
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new[] { "value1", "value2" };
}
//HEAD api/values
public void Head()
{
}
//DELETE api/values
public void Delete()
{
}
}
class Program
{
static void Main(string[] args)
{
var baseAddress = "http://localhost:9000/";
// Start OWIN host
using (WebApp.Start(baseAddress, Configuration))
{
Console.WriteLine("Host started");
Console.ReadLine();
}
}
public static void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
}
}
有效的示例请求:
Invoke-WebRequest -Uri 'http://localhost:9000/api/values' -Method DELETE
这一点已经讨论过了。看来他们希望你在你这边处理
Invoke-WebRequest -Uri 'http://localhost:9000/api/values' -Method DELETE