【发布时间】:2019-02-03 17:51:12
【问题描述】:
我正在开发一个 API,目前所有页面都返回 JSON,除了 404 页面,它是默认的 ASP.Net 404 页面。我想更改它,以便它在 404 页面上也返回 JSON。像这样的:
{"Error":{"Code":1234,"Status":"Invalid Endpoint"}}
如果我在 Global.asax.cs 文件中捕获 404 错误并重定向到现有路由,我可以获得类似的效果:
// file: Global.asax.cs
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
Response.Redirect("/CatchAll");
}
}
// file: HomeController.cs
[Route("CatchAll")]
public ActionResult UnknownAPIURL()
{
return Content("{\"Error\":{\"Code\":1234,\"Status\":\"Invalid Endpoint\"}}", "application/json");
}
但这会向新 URL 返回一个 302 HTTP 代码,这不是我想要的。 我想在正文中返回一个带有 JSON 的 404 HTTP 代码。我该怎么做?
我尝试过的方法都没有用...
#1 - 覆盖默认错误页面
我想也许我可以覆盖 404 处理程序中的默认错误页面。我试过这样:
// file: Global.asax.cs
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
Response.Clear();
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
Response.AddHeader("content-type", "application/json");
Response.Write("{\"Error\":{\"Code\":1234,\"Status\":\"Invalid Endpoint\"}}");
}
}
但它只是继续提供默认的 ASP 错误页面。顺便说一句,我根本没有修改我的web.config 文件。
#2 - 使用“包罗万象”的路线
我尝试在路由表的末尾添加一条“包罗万象”的路由。我的整个路线配置现在看起来像这样:
// file: RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "NotFound",
url: "{*data}",
defaults: new { controller = "Home", action = "CatchAll", data = UrlParameter.Optional }
);
}
但这也不起作用。如果我在Application_Error() 中放置一个断点,我可以看到它仍然以 404 错误代码结束。我不确定这怎么可能,因为应该匹配所有路线?但无论如何,它永远不会到达包罗万象的路线。
#3 - 在运行时添加路由
我在另一个 SO 问题上看到了 this answer,在该问题上可以从错误处理程序中调用新路由。所以我尝试了:
// file: Global.asax.cs
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Home");
routeData.Values.Add("action", "CatchAll");
Server.ClearError();
Response.Clear();
IController homeController = new HomeController();
homeController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
但是它给出了以下错误:
System.Web.Mvc.dll 中出现“System.Web.HttpException”类型的异常,但未在用户代码中处理
附加信息:在控制器
Myproject.Controllers.HomeController上找不到公共操作方法“CatchAll”。
控制器肯定有这个方法。我正在使用 POST 调用 API,但是我尝试通过在方法前添加 [HttpPost] 来制作专门用于发布的控制器,但我仍然遇到相同的错误。
【问题讨论】:
标签: c# json asp.net-mvc api http-status-code-404