WebApiConfig.cs如何在HTTP返回定製的Json 404個的ASP.NET Web API
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "search/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//JSON RESPONSE SETTING
config.Formatters.Clear();
config.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());
config.EnableSystemDiagnosticsTracing();
}
ReplyController.cs
public class ReplyController: ApiController
{
[HttpGet]
public HttpResponseMessage getReply()
{...}
}
,我下面寫的JSON返回的自定義錯誤代碼:
401 Bad Request - {meta:{code:401, msg='invalid params'}}
我想爲404 http狀態編寫json返回碼。
我在哪寫?控制器類? webconfig.cs? Global.asax中?