【问题标题】:WCF service. How can I return custom error/info pageWCF 服务。如何返回自定义错误/信息页面
【发布时间】:2018-12-17 09:43:02
【问题描述】:

我有 WCF 服务。如何删除默认的 WCF 信息/错误页面。并返回自己的自定义页面或 JSON 结果?

例如 WCF 默认信息页面

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    也许你可以使用 iis 的 Application_EndRequest 事件。

       protected void Application_EndRequest(object sender, EventArgs e)
        {
            // you could decide whether to redirect according to the path and status code
           string path = HttpContext.Current.Request.Path;
            if (HttpContext.Current.Response.StatusCode == 400)
            {
                HttpContext.Current.Response.Redirect("/ErrorPage.aspx");
            }
    
        }
    

    请不要忘记确保 wcf 在兼容模式下运行。

     <serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
    

    在您的服务之上

     [AspNetCompatibilityRequirements(RequirementsMode =AspNetCompatibilityRequirementsMode.Allowed)]
    public class CalculatorService : ICalculatorService
    

    否则 Application_EndRequest 事件不会触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多