- When using UriTemplate in WebInvoke attribute, all parameters should be of string type.
- In the web.config, don't forget to add serviceBehavior and endpointBehavior.
- The endpointBehavior needs to have <webHttp /> and not <enableWebScript />.
- When using WCF for Ajax client (i.e., $.ajax()), keep in mind that Ajax client only knows "GET" and "POST" and not "PUT" or "DELETE".
IService2.cs
[OperationContract] [WebInvoke(Method ="GET", UriTemplate = "/CategoryName/{id}", ResponseFormat = WebMessageFormat.Json, RequestFormat =WebMessageFormat.Json)] string GetCategoryName(string id);
Service2.svc.cs
public class Service2 : IService2 public string GetCategoryName(string id) { string retVal = "NotFound"; int categoryID = Convert.ToInt32(id); using (NorthwindEntities db = new NorthwindEntities()) { var category = db.Categories.Find(categoryID); if(category != null) { retVal = category.CategoryName; } } return string.Format("Category with ID {0} is {1}.", categoryID, retVal); } }
web.config
No comments:
Post a Comment