Thursday, December 13, 2012

WCF REST API and Optional Parameters


In case you are developing WCF REST API with GET operation and there is need to have provision for sending optional parameters, this post is for you. Earlier Microsoft doesn't support this however with .NET Framework 4.0 this works smoothly. I will show you how:

For example there are three parameters(id, language, mode) that are always passed in the url and other one(type) is optional.

Request URL could be like this:
or

Handling at Service side will be as follows:

 [ServiceContract]
 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
   
    public class MyService
    {
        [WebGet(UriTemplate = "?id=ID&language=LanguageCode&mode=free")]
        public string GetValue(string id, string language, string mode)
        {
            var type = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["type"];
            if(type!=null)
     {
return "Hi There " + ID + " " + LanguageCode + " " + mode + " " + type;
     }
     else
     {
return "Hi There " + ID + " " + LanguageCode + " " + mode;
     }

        }
    }

Request URL could be like this:
or

Handling at Service side will be as follows:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
   
    public class MyService
    {
        [WebGet(UriTemplate = "id/{ID}/language/{LanguageCode}/mode/{mode}")]
        public string GetValue(string ID, string LanguageCode, string mode)
        {
            var type = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["type"];
            if(type!=null)
     {
return "Hi There " + ID + " " + LanguageCode + " " + mode + " " + type;
     }
     else
     {
return "Hi There " + ID + " " + LanguageCode + " " + mode;
     }
        }
    }

Hope it helps you.

Friday, November 23, 2012

HTTPWebRequest “DELETE” error status 405 Method not allowed in IIS7/IIS7.5

I created a REST API and used REST Client to test the DELETE call and I had an error "Status Code 405 Method Not Allowed". I tried using the IIS 7 and 7.5 I've tried all solutions that I can found in the forums for 1 day, re-register ASP.NET into IIS. But none of the solutions work in my case. Below is the error details I found:

MODULE_SET_RESPONSE_ERROR_STATUS
ModuleName  StaticFileModule
Notification    128
HttpStatus  405
HttpReason  Method Not Allowed
HttpSubStatus   0
ErrorCode   2147942401
ConfigExceptionInfo    
Notification    EXECUTE_REQUEST_HANDLER
ErrorCode   Incorrect function. (0x80070001)
    MODULE_SET_RESPONSE_ERROR_STATUS
Warning    
ModuleName="StaticFileModule", Notification="EXECUTE_REQUEST_HANDLER", HttpStatus="405", HttpReason="Method Not Allowed", HttpSubStatus="0", ErrorCode="Incorrect function

 There are a couple of options to fix this problem:
1) Uninstall WebDAV from the server entirely. You can do this from the Windows Add/Remove features app. This will require a reboot.

2) The second solution is simple. 
A) Go to the IIS Site and click models. Find the WebDAV module and remove it.
Now you can still use WebDAV on your other sites and not interfere with the DELETE method on this site.


B) You may need to find the correct handler mapping and add the DELETE verb