Ajax Url to Routed WCF in Asp.Net 4 Web Forms

437 Views Asked by At

I implemented Routing in Asp.Net 4 Web App. With aspx file it works fine, but it doesn't work with WCF.

I have WCF which is called from javascipt file by ajax request.

My WCF code is:

[ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Signature
    {

        /// <summary>
        /// Get signature info by email address.
        /// </summary>        
        [OperationContract]        
        [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
        public string GetSignatureInfo(string email)
        {
            ...
        }
    }
}

Web.config:

<services>     
  <service name="WEB.Services.Signature">
    <endpoint address="" behaviorConfiguration="WEB.Services.SignatureAspNetAjaxBehavior"
      binding="webHttpBinding" contract="WEB.Services.Signature" />
  </service>
</services>

javascript:

var _data = {
    "email": self.email.val()
};

$.ajax({
    url: '../Signature'
    , type: 'POST'
    , dataType: 'json'
    , data: JSON.stringify(_data)
    , success: function (data) {
        console.log('success');
    }
    , error: function () {
        console.log('error');
    }
});

Global.asax:

void RegisterRoutes(RouteCollection routes)
{
    var sr = new ServiceRoute("Signature", new WebServiceHostFactory(), typeof(Signature));
    sr.Url = "Services/Signature.svc/GetSignatureInfo";
    routes.Add(sr);
}


void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup

    RegisterRoutes(RouteTable.Routes);            
}

I get "NetworkError: 404 Not Found - _http://www.my-project.com/Signature". Where am I wrong or what ajax url should be ?!!! Please, help. Thanks in advance.

1

There are 1 best solutions below

0
On

My work colleague has found a solution: The problem was in IIS configuration - If my app is under Default Site , I should add ajax url with prefix of project. For example, if my Service is located in Web project, I should enter url like : '/Web/Services/MyService/MyFunction' in js file. The same url should be on Global.asax file, in ServiceRoute url, but without first '/'. In this case, it will work fine locally,but there will be a problem to put it on production server.

The optimal solution: 1. IIS : add web site, configure physical path to him, change its port(!!!). 2. .js + Global.asax : just remove project name from url in both places. So, the url will be like : in js file '/Services/MyService/MyFunction' and in Global.asax 'Services/MyService/MyFunction' (without first '/')

That's all. Big thanks to Miri(colleague).