【发布时间】:2026-02-06 01:25:01
【问题描述】:
我使用 Owin 自托管和 WebApi 编写了简单的服务器:
namespace OwinSelfHostingTest
{
using System.Threading;
using System.Web.Http;
using Microsoft.Owin.Hosting;
using Owin;
public class Startup
{
public void Configuration(IAppBuilder builder)
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"Default",
"{controller}/{id}",
new { id = RouteParameter.Optional }
);
builder.UseWebApi(config);
}
}
public class Server
{
private ManualResetEvent resetEvent = new ManualResetEvent(false);
private Thread thread;
private const string ADDRESS = "http://localhost:9000/";
public void Start()
{
this.thread = new Thread(() =>
{
using (var host = WebApp.Start<Startup>(ADDRESS))
{
resetEvent.WaitOne(Timeout.Infinite, true);
}
});
thread.Start();
}
public void Stop()
{
resetEvent.Set();
}
}
}
当控制器出现异常时,Owin 会返回如下 XML 响应:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Attempted to divide by zero.</ExceptionMessage>
<ExceptionType>System.DivideByZeroException</ExceptionType>
<StackTrace>
...
</StackTrace>
</Error>
但我想要不同的输出 - 那么我该如何覆盖它呢?
【问题讨论】:
-
你在寻找什么样的输出?
标签: c# exception asp.net-web-api owin