Use jquery / ajax to call a C # (or any other .net) function in another project within the same solution

Does anyone know if jquery / ajax can be used to call a C # (or any other .net) function in another project within the same solution?

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

.

I've tried following in .js file

in Project.Source

:

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

      

Then I changed the url string in a couple of ways, but it never worked.

Do you have any tips?

+3


source to share


3 answers


Thanks everyone for your quick responses.

I found a solution to the problem and I would like to share it just in case someone runs into the same problem in the future.

In the .js file, before calling the $ .ajax function, I create a variable using window.location that points to the target function url of the running target project and uses the variable in the ajax function. Therefore, you are not pointing to another project. You provide the URL to launch the project.



As simple as it sounds.

Below is the solution:

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

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

      

+1


source


You can only call functions in code because they are registered by the web server.

If you want a function to be available outside of your code, it must be registered as an ASMX or WCF service.

See Creating and Using Your First WCF Service to Configure a WCF Service.



Once installed and running, you can use Ajax to call methods just like you did in 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: Using WCF from jQuery as JSON

0


source


The short answer is, "No, that's impossible." The front-end code (like jQuery and AJAX) runs on the client machine, but C # functions run on the server and run on the server. There is no direct connection between them.


Longer answer: "Not directly, but there are ways to do something like this." The 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 have separated a little. Instead of the url being a function, the url should be the actual url on your website. Using /Functions/CallTargetFunction

as an example, you should create a controller like this:

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

      

Note that this means that anyone who visits http://yoursite.com/Functions/CallTargetFunction

will receive the result of this function.

0


source







All Articles