【问题标题】:Customize Web Api Error message when url not matchedurl 不匹配时自定义 Web Api 错误消息
【发布时间】:2017-04-03 10:28:54
【问题描述】:

我有一个托管在 IIS 上的 web api,它可以通过互联网访问。当我在请求url中输入错误的参数时,它显示的是api服务器的ip地址而不是它的地址。 示例:我在 abc.com 上托管 api。我通过 abc.com/api/myapi?par=kk 访问 api 当我请求错误的 api 时,它显示 No HTTP resource was found that matches the request URI 'https://10.10.10.10/api/myapi 我希望它显示为 abc.com/api/myapi Web api 或 IIS 上是否有任何配置?

【问题讨论】:

    标签: api iis asp.net-web-api2


    【解决方案1】:

    您可以在 Global.asax 中设置自定义的 Not Found 页面

    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404)
        {
         var urlRef = Context.Request.Url;
         Response.Clear();
    
         //Set your route details here 
         var routedata = new RouteData();
         routedata.Values["controller"] = "Home";//Your controller
         routedata.Values["action"] = "Index";// Your action
    
         //Redirect to not found page or login page
         //Sample logic
         IController c = new HomeController();
         c.Execute(new RequestContext(new HttpContextWrapper(Context), routedata));
        }
    }
    

    使用自定义视图,这样您想显示的内容就会出现在您的视图中

    【讨论】:

      猜你喜欢
      • 2012-08-29
      • 2013-11-04
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 2013-08-17
      相关资源
      最近更新 更多