我有一个异常处理程序,
public class MyExceptionHandler : ExceptionHandler
{
public override void Handle(ExceptionHandlerContext context)
{
var content = new { message = context.Exception.Message };
var jsonContent = JsonConvert.SerializeObject(content, Formatting.Indented);
var httpResponseMessage = new HttpResponseMessage
{
Content = new StringContent(jsonContent, Encoding.UTF8, "application/json")
};
switch (context.Exception)
{
case InvalidServiceRequestException _:
httpResponseMessage.StatusCode = HttpStatusCode.BadRequest;
break;
case EntityNotFoundException _:
httpResponseMessage.StatusCode = HttpStatusCode.NotFound;
break;
default:
httpResponseMessage.StatusCode = HttpStatusCode.InternalServerError;
break;
}
context.Result = new MyActionResult(httpResponseMessage);
}
}
然后在管道中设置此处理程序,
private static void ConfigureErrorHandler(HttpConfiguration config)
{
config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());
}
我这样设置cors:
private static void ConfigureCors(HttpConfiguration config)
{
var corsAttribute = new EnableCorsAttribute("http://127.0.0.1:14725", "*", "*");
config.EnableCors(corsAttribute);
}
这是什么问题?