Roh*_*ner 4 asp.net-web-api owin katana
我有一些时间做研发,并且到目前为止一直在玩OWIN.
我想为所有数据交互运行一个OWIN WebAPI服务,并使用一个单独的Web前端SPA项目来使用angular.
所有代码都是从各种随机博客帖子中无耻地窃取的,它只是为了掌握这种"新技术".
启动
public class Startup
{
public void Configuration(IAppBuilder app)
{
#if DEBUG
app.UseErrorPage();
#endif
app.UseWelcomePage("/");
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
app.UseWebApi(config);
app.Run(context =>
{
if (context.Request.Path.ToString() == "/fail")
{
throw new Exception("Random exception");
}
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("App Init");
});
}
}
Run Code Online (Sandbox Code Playgroud)
AccountsController
public class AccountsController : ApiController
{
// GET api/<controller>/5
public string Get(int id)
{
throw new Exception("Random exception");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我导航到 [http:// localhost:85/fail],我会得到一个非常性感的错误页面.
但是当我点击[http:// l0calhost:85/api/accounts/5]时,错误被公开为json/xml.
目前(版本5.0)Web API没有办法强制其中发生的异常在管道中进一步传播.您已经注意到它当前将异常转换为具有json/xml内容的http响应消息.但是在即将发布的版本中,我们将提供此功能作为我们的全局错误处理功能的一部分,您可以在其中插入错误处理机制.
IMO看到Web API错误的错误页面并不是很有用.如果您的客户端是进行这些调用的Javascript,那么该html应该如何显示?此外,目前浏览器具有良好的调试支持,您可以在其中跟踪调用并从响应中找出问题.
| 归档时间: |
|
| 查看次数: |
2678 次 |
| 最近记录: |