具有控制器操作名称的Web API路由前缀

时间:2020-07-16 11:17:45

标签: c# jquery ajax asp.net-core asp.net-core-webapi

我有一个asp.net core 3.1应用程序。在启动文件中,我有以下路线。

  app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action}/{id?}",
                defaults: new { controller = "Home", action = "Index" });
            endpoints.MapControllers();
            endpoints.MapRazorPages();
        });

我有带索引和详细信息视图的位置控制器。

 public class PositionController : Controller
{
    private readonly IPositionRepository repository;

    public Position Position { get; set; }

    public PositionController(IPositionRepository repository)
    {
        this.repository = repository;
    }
    
    public IActionResult Index()
    {
        return View();
    }

    public async Task<IActionResult> Details(int id)
    {
        if (id < 0)
            return View();

        Position = await repository.FindPositionById(id);

        if (Position == null)
           return RedirectToAction("_NotFound");

        return View(Position);
    }

索引视图指向https:// localhost:44356 / Position,“详细信息”视图指向https:// localhost:44356 / Position / 1。 (位置ID为1)

我有一个Web API,可通过Get和GetTaskGroups操作获取所有位置信息。

[Produces("application/json")]
[Route("api/positions")]
[ApiController]
public class PositionsController : Controller
{
    private readonly IPositionRepository repository;

    public PositionsController(IPositionRepository repository)
    {
        this.repository = repository;
    }

    [HttpGet]
    public async Task<IActionResult> Get()
    {
        return Json(new { data = await repository.GetPositions() },
            new JsonSerializerSettings() { MetadataPropertyHandling = MetadataPropertyHandling.Ignore 
                         });
    }

    [HttpGet("{posId}/tasks")]
    public async Task<IActionResult> GetTaskGroups(int posId)
    {
        return Json(new { data = await repository.GetTaskGroups(posId) });
    }

我正在使用jQuery ajax调用来获取数据,如下所示。

function loadPositions() {
positions =
    $('#positionsList').DataTable({
        "ajax": {
            "url": "api/positions",
            "type": "get",
            "datatype": "json"
        },...

function loadTaskGroups(posId) {
taskGroups =
    $('#taskGroups').DataTable({
        "ajax": {
            "url": `/api/positions/${posId}/tasks`,
            "type": "get",
            "datatype": "json"
        },...

我可以为“索引”视图获取数据,但不能为“详细信息”视图获取数据。 Web api路由指向https:// localhost:44356 / Position / Details / 1 / api / positions / 1 / tasks,该地址应指向https:// localhost:44356 / api / positions / 1 / tasks。原因是我使用以下路由https:// localhost:44356 / api / positions /

获取Index视图的数据

我认为,问题与默认路由有关。索引视图不会显示在url中,但详细信息视图会显示。

2 个答案:

答案 0 :(得分:1)

使用您的代码,我确实重现了您的问题。

您只需要通过/方法删除 ajax网址中的loadTaskGroups

function loadTaskGroups(posId) {
taskGroups =
    $('#taskGroups').DataTable({
        "ajax": {
            "url": `api/positions/${posId}/tasks`,
            "type": "get",
            "datatype": "json"
        },...

这是测试结果:

enter image description here

答案 1 :(得分:-1)

尝试

function loadPositions() {
positions =
    $('#positionsList').DataTable({
        "ajax": {
            "url": "/api/positions",
            "type": "get",
            "datatype": "json"
        },...
相关问题