locked
WCF Post Method Problem in accessing from browsers RRS feed

  • Question

  • User1267394301 posted

    Hi,

    We are having strange issues while trying to access RESTful Service using web browser. Details of which are given below:

     

    Things we need to achieve:

    i)                    Create a RESTful  WCF POST service which will accept parameters as input in pure JSON format and send response in JSON format.

    ii)                   Access the service from different web browsers and test it from mobile device as it is a mobile application

     

    Things we have done so far:

    i)                    Created a AJAX enabled WCF Post service in .NET 4.0 with Operation Contract set as

    [OperationContract, WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

     

    ii)      ASPNET Compatibility option is set to Allowed for the service.

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

     

    iii)    WCF Service signature is

    public UPCData SaveUPCData(List<UPCEntries> UPCList, string PONumber, string StoreCode, string ProgramName, string User)

     

    To support CORS, changes are done in Global.asax file. 

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
    //HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    //HttpContext.Current.Response.Cache.SetNoStore();

    //EnableCrossDmainAjaxCall();
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
    HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
    HttpContext.Current.Response.End();
    }
    }

     

    The service is hosted in web server and while trying to access the service using test HTML page we are getting “GET http://172.26.14.206/InStoreMobilityP/SaveUPC.svc/SaveUPCData [HTTP/1.1 405 Method Not Allowed 793ms]” error in firefox browser. 

    Please let me know solutions as I have treid all the methods possible from google.

    Regards,

    Anjan

    Wednesday, May 28, 2014 10:37 AM

Answers

  • User-417640953 posted

    [OperationContract, WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

    public UPCData SaveUPCData(List<UPCEntries> UPCList, string PONumber, string StoreCode, string ProgramName, string User)

    Hi anjansingh,

    Thanks for your post.

    Generally, the http 405 status [method not allowed] always occur with the post method.

    From your code provided, I think the issue come from the wrong json format you post to rest service.

    Please note if service method contains more than one parameters, we should not pass pure json format to it.

    We should pass the json data with wrapped format.

    http://stackoverflow.com/questions/20206069/restful-web-service-body-format

    If you still cannot solve it, please share simple demo code with us and let us reproduce your scenes.

    Thanks.

    Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 29, 2014 1:03 AM

All replies

  • User-484054684 posted

    Hi Anjan

    Could you post your application's config file with the endpoint and other behaviors information.

    Please make sure you have webHttp behavior listed for your endpoint.

    Regards

    Siva

    Wednesday, May 28, 2014 10:55 AM
  • User1267394301 posted

    Hi Siva, this is the code snippet from web.config file. The service is SaveUPC service.

    <system.webServer>

    <directoryBrowse enabled="true" />
    <modules runAllManagedModulesForAllRequests="true" />
    <httpProtocol>
    <customHeaders>
    <add name="Access-Control-Allow-Origin" value="http://172.26.14.206/" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="POST,OPTIONS" />

    </customHeaders>
    </httpProtocol>
    </system.webServer>

    <system.serviceModel>
    <bindings>
    <webHttpBinding>
    <binding name="webHttpBinding" crossDomainScriptAccessEnabled="true"/>
    </webHttpBinding>
    </bindings>
    <standardEndpoints>
    <webScriptEndpoint>
    <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
    </webScriptEndpoint>
    </standardEndpoints>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
    multipleSiteBindingsEnabled="true" />

    <behaviors>
    <endpointBehaviors>
    <behavior name="Mobility_Pilot.SaveUPCAspNetAjaxBehavior">
    <enableWebScript/>
    </behavior>
    <behavior name="web">
    <webHttp/>
    </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
    <behavior name="ServiceBehaviour">
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    </serviceBehaviors>
    </behaviors>

    <services>

    <service name="Mobility_Pilot.GetPODetails" behaviorConfiguration="ServiceBehaviour">

    <endpoint address="" binding="webHttpBinding" contract="Mobility_Pilot.IGetPODetails" behaviorConfiguration="web" ></endpoint>
    </service>

    <service name="Mobility_Pilot.SaveUPC" behaviorConfiguration="ServiceBehaviour">
    <endpoint address="" behaviorConfiguration="Mobility_Pilot.SaveUPCAspNetAjaxBehavior"
    binding="webHttpBinding" contract="Mobility_Pilot.SaveUPC" />

    </services>

    </system.serviceModel>
    </configuration>

    Wednesday, May 28, 2014 4:15 PM
  • User-417640953 posted

    [OperationContract, WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

    public UPCData SaveUPCData(List<UPCEntries> UPCList, string PONumber, string StoreCode, string ProgramName, string User)

    Hi anjansingh,

    Thanks for your post.

    Generally, the http 405 status [method not allowed] always occur with the post method.

    From your code provided, I think the issue come from the wrong json format you post to rest service.

    Please note if service method contains more than one parameters, we should not pass pure json format to it.

    We should pass the json data with wrapped format.

    http://stackoverflow.com/questions/20206069/restful-web-service-body-format

    If you still cannot solve it, please share simple demo code with us and let us reproduce your scenes.

    Thanks.

    Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 29, 2014 1:03 AM