WCF服务。如何返回自定义错误/信息页面

时间:2018-12-17 09:43:02

标签: c# wcf

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

例如WCF默认信息页面

enter image description here

1 个答案:

答案 0 :(得分:0)

也许您可以使用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事件将不会触发。