Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无require参数的MVC4JSON路由Post数据路由_C#_Jquery_Asp.net Mvc 4_Http Post_Asp.net Mvc Routing - Fatal编程技术网

C# 无require参数的MVC4JSON路由Post数据路由

C# 无require参数的MVC4JSON路由Post数据路由,c#,jquery,asp.net-mvc-4,http-post,asp.net-mvc-routing,C#,Jquery,Asp.net Mvc 4,Http Post,Asp.net Mvc Routing,在MVC4路线中,我有如下路线: RouteTable.Routes.MapRoute( name: "Comments_InsertComment", url: "{controller}/{action}/{parameters}", defaults: new { controller = "Comments", action = "InsertComment", parameters = UrlPar

在MVC4路线中,我有如下路线:

        RouteTable.Routes.MapRoute(
            name: "Comments_InsertComment",
            url: "{controller}/{action}/{parameters}",
            defaults: new { controller = "Comments", action = "InsertComment", parameters = UrlParameter.Optional}
        );
控制器上的方法具有如下签名:

public ActionResult InsertComment(AddParameters parameters) {
    //parameters are passed in via jquery ajax post data
}
            $.ajax({
            type: 'POST',
            url: '/Comments/InsertComment/null',
            data:params,
            dataType: "json",
            error: function (err) {
                alert("error - " + err);
            },
            success: function (data) {
                console.log(data);
                alert('Your comment was added!');
            }
        });
我用ajax这样称呼他们

public ActionResult InsertComment(AddParameters parameters) {
    //parameters are passed in via jquery ajax post data
}
            $.ajax({
            type: 'POST',
            url: '/Comments/InsertComment/null',
            data:params,
            dataType: "json",
            error: function (err) {
                alert("error - " + err);
            },
            success: function (data) {
                console.log(data);
                alert('Your comment was added!');
            }
        });
我遇到的问题是我必须在url中指定/null或/anything,否则它找不到它。当我希望它只是/Comments/InsertComment,然后是post数据时

我已经讨论了这里可以找到的大多数类似的路由问题,但是没有一个是通过ajax调用将json对象发布到控制器上的(这很有效)

我已尝试将{parameters}设置为{id},并将id=urlparmeter.Optional设置为。我试着把它完全排除在路线之外。我甚至尝试了parameters=urlparmeter.Optional

我敢肯定,它与其他任何路线都不匹配

编辑:

AddParameters定义为:

public class AddCommentParameters
{
    public string ParentCommentId { get; set; }
    public string CommentText { get; set; }
    public string BlockReplies { get; set; }
}
参数在javascript中设置为

var params = { ParentCommentId: null, CommentText: commentText, BlockReplies: null };

请尝试在ajax中使用此代码块

url: '@Url.Content("~/Comments/InsertComment")',
data: { parameters: params },
dataType: 'JSON'

我希望这会有所帮助。

请尝试在ajax中使用此代码块

url: '@Url.Content("~/Comments/InsertComment")',
data: { parameters: params },
dataType: 'JSON'

我希望这会有所帮助。

它需要是
url:'/Comments/InsertComment',
(或者更好的
url:'@url.Action(“InsertComment”,“Comments”),
什么是
params
(在
数据中:params
)?typeof
AddParameters
的属性是什么?-它们需要匹配才能用请求的详细信息更新我的问题。我之所以不使用@Url.Action…是因为javascript是一个独立的文件。我有一个注册路由实用程序,可以在任何使用评论系统的站点上调用。我还使用“/Com”“ments/InsertComment/null”因为如果我在最后没有指定参数,它就不起作用,不管我指定什么,我只需要指定一些东西。我确实找到了一个处理它的默认路由,所以我将其更改为更具体的路由,不处理注释,但问题仍然存在。处理单独的js文件问题的最佳方法是使用dat主视图元素中触发调用的属性(例如,
在我的测试应用程序中摆脱默认路由)似乎已经修复了它。/Comments/InsertComment正在工作。我只是想弄清楚为什么{controller}/{action}将controller设置为main,将action设置为{index}正在为一个名为Comments的控制器和一个名为InsertComment的方法触发。它需要是
url:'/Comments/InsertComment',
(或者更好的
url:'@url.Action(“InsertComment”,“Comments”),
什么是
params
(在
数据中:params
)?typeof
AddParameters
的属性是什么?-它们需要匹配才能用请求的详细信息更新我的问题。我之所以不使用@Url.Action…是因为javascript是一个独立的文件。我有一个注册路由实用程序,可以在任何使用评论系统的站点上调用。我还使用“/Com”“ments/InsertComment/null”因为如果我在最后没有指定参数,它就不起作用,不管我指定什么,我只需要指定一些东西。我确实找到了一个处理它的默认路由,所以我将其更改为更具体的路由,不处理注释,但问题仍然存在。处理单独的js文件问题的最佳方法是使用dat主视图元素中触发调用的属性(例如,
在我的测试应用程序中摆脱默认路由)似乎已经修复了它。/Comments/InsertComment正在工作。我只是想弄清楚为什么{controller}/{action}将controller设置为main,将action设置为{index}正在为名为Comments的控制器和名为InsertComment的方法触发。。