how to turn off the auto-redirect feature in WCF REST 4.0

Missing a trailing slash is a common error people make when browsing the web. For better usability, in 4.0, we introduced a feature called autoredirect. It is on by default, meaning, when you set up your REST service to take something like "test/", when user types in https://hostname/ServiceName/test in the browser, they will be automatically redirected to https://hostname/ServiceName/test/.

 [ServiceContract]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

public class EchoService

{

[

WebGet(UriTemplate="test/")]

public string Echo()

{

return "hello";

}

}

If for some reason, you want to turn this feature off, you can register a message inspector to do that.

public

class MyMessageInspector : IDispatchMessageInspector

{

internal const string RedirectPropertyName = "WebHttpRedirect";

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)

{

// remove the RedirectPropertyName from the request

if ( request.Properties.ContainsKey(RedirectPropertyName))

{

request.Properties.Remove(RedirectPropertyName);

}

return null;

}

public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)

{

}

}

Hope this helps!