使用ajax调用主控制器操作
我在我的家庭控制器中对'url'进行响应的'Index'操作。 该动作还有一个可选的“ID”参数...
[HttpGet]
public ViewResultBase Index(int id = -1)
{
     ....
}
这个路由是...
routes.MapRoute("Home",
                "",
                 new { controller = "Home", action = "Index", id = -1 } );
如果我尝试使用ajax调用此操作...
    $.get(
        '@Url.Action("Index", "Home")',
        { id: 20 },
        function (response) {
        });
该呼叫失败,因为该网址为/?id = 20
是否有可能强制Url.Action包含控制器和动作名称,因为当我这样做...
'@ Url.Action(“Index”,“Home”)'+'Home / Index'
一切工作正常,还是我需要纠正我的路由?
你不需要像这样为你的Action方法制作一个特殊的路线。 默认路由:
 {controller}/{action}/{id}, 
 new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  已处理该案件;  当你在服务器上请求'/'时,你应该发送到你的Home/Index操作方法。  您应该删除您指定的自定义路由,并让默认路由将您发送到正确的目的地。 
上一篇: Calling the home controller action using ajax
下一篇: ASP.NET MVC Url.Action adds current route values to generated url
