0

I have a WCF service configured and I'm using routing to configure it. Everything is working the way I want it, except the 404 messages have a body stating Service Endpoint not found.

I'd like the 404 to have an empty response body.

Here is my route registration:

public class Global : HttpApplication
{

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);

    }

    private void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(new ServiceRoute("RootService", new WebServiceHostFactory(), typeof(ServiceProvider)));
    }

Here is my service class:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceContract]
public class ServiceProvider 
{
    [WebGet]
    public Test ValidUrl()
    {
        return new Test();
    }
}

How do I make the response for this url http://localhost/RootService have an empty 404 body?

David Silva Smith
  • 11,498
  • 11
  • 67
  • 91
  • Try writing a MessageInspector that intercepts your request and accordingly returns your response – Rajesh Apr 27 '12 at 09:36

1 Answers1

0

I found a few ways to do this and I've listed two below. They key is having the UriTemplate set as *. This makes the method match all routes that aren't explicitly matched otherwise.

    [WebGet(UriTemplate="*")]
    public void ErrorForGet()
    {
        throw new WebFaultException(HttpStatusCode.NotFound);
    }

I don't like this way as well, but it works:

    [WebGet(UriTemplate="*")]
    public void ErrorForGet()
    {
        WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
    }

Both of these methods have overloads that take a string as a message to provide to the requesting client. The WebFaultException needs to be like this going that route though: throw new WebFaultException<string>("Resource not found", HttpStatusCode.NotFound);

David Silva Smith
  • 11,498
  • 11
  • 67
  • 91