Ajax调用区域控制器

时间:2017-09-10 20:40:15

标签: ajax asp.net-mvc asp.net-core

我试图从Ajax调用一个Controller,但我得到了:

  

无法加载资源:服务器响应状态为404()

Ajax正在从Admin View调用Admin Controller。我可以查看管理页面,但我无法从Ajax或任何其他方法调用GetAll()和GetAllUsers()。

在初创公司

        app.UseMvc(routes =>
        {

            routes.MapRoute("areaRoute",template:"{area:exists}/{controller=Admin}/{action=Index}/{id?}");

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

在AdminController中

namespace Project.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Authorize(Policy = "AdminOnly")]
    public class AdminController : Controller
    {    
        public IActionResult Index()
        {
            return View();
        }
        public AdminController()
        {        
        }
        [HttpGet("GetAll/{page}")]
        public IActionResult GetAll(int? page)
        {           
        }
        [HttpGet("GetAllUsers/{page}")]
        public IActionResult GetAllUsers(int? page)
        {         
        }
    }
}

的Ajax:

@section Scripts{ 
    @await Html.PartialAsync("_ValidationScriptsPartial")
    <script>
        $(function () {
            $.ajax({
                type: "GET",
                url: "/Admin/GetAll",                
                traditional: true,
                success: function (view) {
                    $("Result").html(view)
                }
            });

            $.ajax({
                type: "GET",
                url: "/Admin/GetAllUsers",
                traditional: true,
                success: function (view) {
                    $("userResult").html(view)
                }
            });
        });
    </script>
}

1 个答案:

答案 0 :(得分:0)

在网址中

您需要使用/ {areaName} / {controllerName} / {ActionMethod}

 $.ajax({
            type: "GET",
            url: "/Admin/Admin/GetAllUsers",
            traditional: true,
            success: function (view) {
                $("userResult").html(view)
            }
        });