.netwcfresturitemplate

Root UriTemplate is getting hijacked by WCF help page


TL;DR - I have [WebGet(UriTemplate = "/")] and it doesn't work


I have WCF service that looks like this:

public interface IUserService
{
    // This doesn't work
    [OperationContract]
    [WebGet(UriTemplate = "/")]
    IList<User> GetAllUsers();

    /////////////////////////////////////
    // Everything below this works
    /////////////////////////////////////

    [OperationContract]
    [WebGet(UriTemplate = "/{id}/")]
    User GetUserById(string id);

    [OperationContract]
    [WebInvoke(UriTemplate = "/", Method = "POST")]
    IList<User> AddUser();

    [OperationContract]
    [WebInvoke(UriTemplate = "/{id}/", Method = "PUT")]
    IList<User> UpdateUser(string id, User user);
}

Here is the configuration for the endpoint

<service name="MyCompany.UserService">
    <host>
        <baseAddresses>
            <add baseAddress="http://localhost:80/api/users/" />
        </baseAddresses>
    </host>
    <endpoint address=""
                        behaviorConfiguration="WebHttpBehavior"
                        binding="webHttpBinding"
                        contract="MyCompany.IUserService" />
    <endpoint address="mex"
                        binding="mexHttpBinding"
                        contract="IMetadataExchange" />
    <endpoint address="soap"
        binding="wsHttpBinding"
        contract="MyCompany.IUserService" />
</service>

As you can see, I am serving both REST and SOAP from this service. This question only deals with REST.

When I go to http://localhost:80/api/users/ in my browser (so GET "/" in WCF terms) I get the WCF help page describing the endpoint - the one that is useful for SOAP but not much help for REST. However, if I do anything else it works as expected. If I POST to this URL, or GET /123456, I get normal JSON responses (as in, it actually executes my service).

It seems that WCF is hijacking the "/" operation. Is there any way to turn off this WCF "help" behavior so that I can execute my operations? Any suggestions are much appreciated.


Solution

  • First, you can disable the service help page by using the following config option:

    <serviceBehaviors>
      <serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false" />
    </serviceBehaviors>
    

    However, this will make the base address return the service wsdl by default. So to move that, you can also use this config option:

    <serviceBehaviors>
      <serviceMetadata httpGetEnabled="true" httpGetUrl="wsdl"/>
    </serviceBehaviors>
    

    This will move the wsdl url to your_service_base_address + "wsdl?wsdl".