【问题标题】:Asp.net MVC Controller endpoint not reachableAsp.net MVC 控制器端点无法访问
【发布时间】:2021-02-04 21:31:10
【问题描述】:

我正在尝试使用 ASP.net MVC 实现服务器端处理,但问题是当我加载我的项目时,只有我的控制器的索引端点被命中,并且我为加载我的项目无法访问的数据指定的路径所以我的数据表中没有加载任何内容。 : Sitefinity 创建的代码不包含 Route.config,因此我无法检查控制器功能的路径,但据我所知,任何控制器的路径都会变成这样“/Controllername/functionname”

控制器:

 [ControllerToolboxItem(Name = "Test", Title = "Test Page", SectionName = "MyCustom")]
    public class TestController : Controller
    {
        // GET: Test
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult LoadData()// I put Breakpoint here and nothing reached 
        {
            TestModel test = new TestModel();
            test.name = "name1";
            test.lastname = "lastnmae1";

            test.name = "name2";
            test.lastname = "lastnmae2";

            return Json(test);

        }
    }

Index.cshtml:

@{
    ViewBag.Title = "Index";
}


<div style="width:90%; margin:0 auto;">
    <table id="myTable">
        <thead>
            <tr>
                <th>name</th>
                <th>lastname</th>
           
            </tr>
        </thead>
    </table>
</div>

@* Load datatable css *@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link href="//cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css" rel="stylesheet" />
@section Scripts{
    @* Load DataTable js here *@

    <script src="//cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#myTable").DataTable({
                "processing": true, // for show progress bar
                "serverSide": true, // for process server side
                "filter": false, // this is for disable filter (search box)
                "orderMulti": false, // for disable multiple column at once
                "ajax": {
                    "url": "/Test/LoadData",
                    "type": "POST",
                    "datatype": "json"
                },
                "columns": [
                    { "data": "name", "name": "name", "autoWidth": true },
                    { "data": "lastname", "name": "lastname", "autoWidth": true },
                        
                ]
            });
        });
    </script>
}

型号:

    public class TestModel
    {
        public string name { get; set; }
        public string lastname { get; set; }

    }

更新
当我通过注释使用自定义路由时也不起作用,我认为原因与this 有关,但问题是我正在使用 Visual Studio IIS 和 localhost 我相信这是由 Sitefinity 引起的,但我不知道如何解决它

【问题讨论】:

    标签: asp.net-mvc model-view-controller datatables sitefinity


    【解决方案1】:

    您是否将小部件添加到具有 /Test url 的页面?

    只有这样你才能期望 /test/loadData 会到达

    【讨论】:

    • 但这不是一个好习惯,我怎么知道小部件将添加到的任何页面名称?
    • 您始终可以使用 Request.Url 获取放置小部件的页面的 url,然后附加操作名称。或者使用更好的 Url.Action("loadData")
    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多