私は R&D を行う時間があり、今日から OWIN をいじっています。
すべてのデータ インタラクションに対して OWIN WebAPI サービスを実行し、Angular を利用する別の Web フロント エンド SPA プロジェクトを使用したいと考えています。
すべてのコードは、さまざまなランダムなブログ投稿から恥知らずに盗まれたものであり、この「新しい技術」を理解するためだけのものです。
起動
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");
});
}
}
アカウントコントローラー
public class AccountsController : ApiController
{
// GET api/<controller>/5
public string Get(int id)
{
throw new Exception("Random exception");
}
}
[http://localhost:85/fail]に移動すると 、非常に魅力的なエラー ページが表示されます。
しかし、[http://l0calhost:85/api/accounts/5]を押すと、エラーが json/xml として公開されます。
- APIコントローラーの例外にAppBuilderエラーメカニズムも使用させる方法はありますか?
- これは眉をひそめますか?(なんか汚い感じ…)