Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't have control over your ajax call I would recommend creating and Interceptor to override the Content-Type Header.</p> <pre><code>public class ContentTypeOverrideInterceptor : RequestInterceptor { public string ContentTypeOverride { get; set; } public ContentTypeOverrideInterceptor(string contentTypeOverride) : base(true) { this.ContentTypeOverride = contentTypeOverride; } public override void ProcessRequest(ref RequestContext requestContext) { if (requestContext == null || requestContext.RequestMessage == null) { return; } Message message = requestContext.RequestMessage; HttpRequestMessageProperty reqProp = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name]; reqProp.Headers["Content-Type"] = ContentTypeOverride; } } </code></pre> <p>Then if you view your .svc file you'll see and AppServiceHostFactory class change it to include the interceptor</p> <pre><code>class AppServiceHostFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var host = new WebServiceHost2(serviceType, true, baseAddresses); host.Interceptors.Add(new ContentTypeOverrideInterceptor("application/json; charset=utf-8")); return host; } } </code></pre> <p>That should do it for you.</p> <h2>Update</h2> <p>As mentioned in the comments the method above is for use with the WCF REST Starter Kit. If you are using just a regular WCF Service you will have to create a IOperationBehavior and attached it to your service. here is the code for the behavior attribute</p> <pre><code>public class WebContentTypeAttribute : Attribute, IOperationBehavior, IDispatchMessageFormatter { private IDispatchMessageFormatter innerFormatter; public string ContentTypeOverride { get; set; } public WebContentTypeAttribute(string contentTypeOverride) { this.ContentTypeOverride = contentTypeOverride; } // IOperationBehavior public void Validate(OperationDescription operationDescription) { } public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) { innerFormatter = dispatchOperation.Formatter; dispatchOperation.Formatter = this; } public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) { } public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters) { } // IDispatchMessageFormatter public void DeserializeRequest(Message message, object[] parameters) { if (message == null) return; if (string.IsNullOrEmpty(ContentTypeOverride)) return; var httpRequest = (HttpRequestMessageProperty)message.Properties[HttpRequestMessageProperty.Name]; httpRequest.Headers["Content-Type"] = ContentTypeOverride; } public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result) { return innerFormatter.SerializeReply(messageVersion, parameters, result); } } </code></pre> <p>And you would have to modify your Service contract to look like this one</p> <pre><code>[OperationContract] [WebContentType("application/json; charset=utf-8")] public string DoWork(string FirstName, string LastName) { return "Your name is " + LastName + ", " + FirstName; } </code></pre> <h2>Links</h2> <p>As you requested here are some links describing these WCF Extensions</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.ioperationbehavior.aspx" rel="noreferrer">IOperationBehavior Interface</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageformatter.aspx" rel="noreferrer">IDispatchMessageFormatter Interface</a></li> <li><a href="http://weblogs.asp.net/vardi/archive/2008/06/18/how-to-get-implementation-of-idispatchmessageformatter.aspx" rel="noreferrer">How to get implementation of IDispatchMessageFormatter</a></li> <li><a href="http://blogs.msdn.com/mahjayar/archive/2007/03/14/wcf-extensibility-plugging-in-custom-message-processing-logic.aspx" rel="noreferrer">WCF Extensibility - Plugging in custom message processing logic</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload