如何从ajax调用通用控制器方法?

如何从ajax调用通用控制器方法?,ajax,asp.net-mvc,getjson,nancy,Ajax,Asp.net Mvc,Getjson,Nancy,我正在asp.net-mvc中使用Nancy框架。在各种视图中,我希望能够通过ajax/getJSON从公共模块调用方法,而不必在每个控制器模块中复制端点,但到目前为止还没有成功 假设我的控制器模块如下所示: namespace SomeNamespace { using Microsoft.EntityFrameworkCore; using Models; using Nancy; public class CommonModule : NancyModul

我正在asp.net-mvc中使用Nancy框架。在各种视图中,我希望能够通过ajax/getJSON从公共模块调用方法,而不必在每个控制器模块中复制端点,但到目前为止还没有成功

假设我的控制器模块如下所示:

namespace SomeNamespace
{
    using Microsoft.EntityFrameworkCore;
    using Models;
    using Nancy;

    public class CommonModule : NancyModule
    {
        public CommonModule(IAppRepository repo)
        {
            Get("/EndpointName/{someargument}", async (x, ct) =>
            {
                string result = string.Empty;
                int someargument;
                if(int.TryParse(x.apptype, out someargument))
                {
                    var data = await repo.SomeModel.AsNoTracking().ToListAsync().ConfigureAwait(false);
                    result = Response.AsJson(data);
                }

                return result;

            });

        }
    }
}
在我的js文件中,我有如下内容:

function callMethod()
{
    var someargument = $('#SomeControl').data('somedata');
    var url = window.location.protocol + '/' + window.location.host + '/CommonModule/EndpointName/' + someargument;
    $.getJSON(url, function (json) {
        //process if/as required;
    })
    .done(function (info) {
        //process if/as required;
    })
    .fail(function (jqxhr, textStatus, error) {
        //process if/as required;
    });

}
$('#CommonModulebtn').click(function () {

$.ajax({
    url: '/CommonModule/CommonModule',
    type: "GET",
    dataType: "JSON",
    data: { IAppRepository : $('#SomeControl').data('somedata').val() },
    success: function (info) {}
这可能吗?如果是,如何做到这一点?我只是构建了错误的url,还是与确保.cshtml文件中存在正确的引用有关?

请尝试以下方法:

function callMethod()
{
    var someargument = $('#SomeControl').data('somedata');
    var url = window.location.protocol + '/' + window.location.host + '/CommonModule/EndpointName/' + someargument;
    $.getJSON(url, function (json) {
        //process if/as required;
    })
    .done(function (info) {
        //process if/as required;
    })
    .fail(function (jqxhr, textStatus, error) {
        //process if/as required;
    });

}
$('#CommonModulebtn').click(function () {

$.ajax({
    url: '/CommonModule/CommonModule',
    type: "GET",
    dataType: "JSON",
    data: { IAppRepository : $('#SomeControl').data('somedata').val() },
    success: function (info) {}
尝试以下方法:

function callMethod()
{
    var someargument = $('#SomeControl').data('somedata');
    var url = window.location.protocol + '/' + window.location.host + '/CommonModule/EndpointName/' + someargument;
    $.getJSON(url, function (json) {
        //process if/as required;
    })
    .done(function (info) {
        //process if/as required;
    })
    .fail(function (jqxhr, textStatus, error) {
        //process if/as required;
    });

}
$('#CommonModulebtn').click(function () {

$.ajax({
    url: '/CommonModule/CommonModule',
    type: "GET",
    dataType: "JSON",
    data: { IAppRepository : $('#SomeControl').data('somedata').val() },
    success: function (info) {}

您可以从应用程序中的任何控制器视图调用任何控制器操作。你的代码很好。您需要确保构造正确的URL

我不熟悉NancyFx路由,但我认为您需要从URL中删除“CommonModule”,使其看起来像:

var url = window.location.protocol + '/' + window.location.host + '/EndpointName/' + someargument;
如果是公用模块,您可能需要更改服务器上的路由:

public CommonModule(IAppRepository repo)
{
     Get("/common/EndpointName/{someargument}", async (x, ct) => ...
JavaScript中的URL将是

var url = window.location.protocol + '/' + window.location.host + '/common/EndpointName/' + someargument;

您可以从应用程序中的任何控制器视图调用任何控制器操作。你的代码很好。您需要确保构造正确的URL

我不熟悉NancyFx路由,但我认为您需要从URL中删除“CommonModule”,使其看起来像:

var url = window.location.protocol + '/' + window.location.host + '/EndpointName/' + someargument;
如果是公用模块,您可能需要更改服务器上的路由:

public CommonModule(IAppRepository repo)
{
     Get("/common/EndpointName/{someargument}", async (x, ct) => ...
JavaScript中的URL将是

var url = window.location.protocol + '/' + window.location.host + '/common/EndpointName/' + someargument;