3

Does anyone know if it's possible to use jquery/ajax to call a c# (or any other .net) function in another project within the same solution?

Let's say that the solution's name is ExampleSolution , the name of the project from which I call the target function is Project.Source and the name of the target project is Project.Target, and the name of the target function is TargetFunction().

I've tried following in an .js file in the Project.Source:

    $.ajax({
            url: '/ExampleSolution/Project.Target/TargetFunction',            
            type: 'get',
            success: function (data) {
                $(document.body).append(data);
            }
   });

Then I've modified the url-line in several ways but never succeed.

Do you have any advice?

laxonline
  • 2,657
  • 1
  • 20
  • 37
kamil.p
  • 41
  • 1
  • 3
  • You need to write something (eg, an MVC action) that accepts HTTP requests and calls the method. – SLaks Jan 22 '13 at 14:18
  • `ScriptMethod` attribute maybe but your server side code will have to handle passing the call over to the library: http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx – asawyer Jan 22 '13 at 14:19

3 Answers3

1

Thank you all for your fast answers.

I found a solution for the problem and I'd like to share it just in case anybody faces the same problem in the future.

In the .js file before I call the $.ajax function I create a variable with help of window.location which points to the url to the targetfunction of the running target-project and use the variable in the ajax-function. So you don't point to another project. You point to url of running project.

Just as easy as it sounds.

Below follows the solution:

    var url = window.location = 'http://localhost:13105/TargetFunction';

    $.ajax({
        url: url,
        type: 'get',
        success: function (data) {
            $(document.body).append(data);
        }
    });
});
kamil.p
  • 41
  • 1
  • 3
0

You can only call functions in the Code Behind because they're being registered by the web server.

If you want to have a function accessible outside the Code Behind it needs to be registered as a ASMX or WCF service.

See Creating and Consuming Your First WCF Service for setting up a WCF Service.

Once it is setup and running you can use Ajax to call the methods just like you would in the Code Behind.

$.ajax({
        //Path to WCF Server and Target Method
        url: "http://localhost:PORT/wcfsvc/FooService.svc/Foo",
        type: 'get',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            $(document.body).append(data);
        }
});

See also: Consuming WCF from jQuery as JSON

Community
  • 1
  • 1
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
0

The short answer is "No, it isn't possible." Front-end code (like jQuery and AJAX) run on the client's machine, but C# functions are back-end and run on the server. There's no direct connection between them.


The longer answer is "Not directly, but there are ways to do something similar." Your easiest option is to use AJAX to POST to another controller/action on your server and then process the response. This is close to what you were doing, but you were slightly off. Rather than the url being a function, the url has to be an actual url on your website. Using /Functions/CallTargetFunction as an example, you would then create a controller like this:

public class FunctionsController : Controller
{
    public ActionResult CallTargetFunction()
    {
        return Content(TargetFunction());
    }
}

Note that doing this means anyone who visits http://yoursite.com/Functions/CallTargetFunction will get the result of that function.

Bobson
  • 13,498
  • 5
  • 55
  • 80