【问题标题】:How to call funtions defined in controller in asp.net core razor pages project如何在asp.net core razor pages项目中调用控制器中定义的函数
【发布时间】:2020-04-02 07:27:42
【问题描述】:

我是 asp.net 核心的新手,我正在探索对控制器中定义的函数进行 api 调用的不同方法。

我有一个基本的新闻控制器如下,如果我在这个控制器中只保留一个函数GetAll(),那么我可以使用https://localhost:44364/api/news成功调用api

当我添加其他两个函数时,相同的 https://localhost:44364/api/news 会收到如下错误消息,这很明显,因为我没有在我的 url 中提及函数名称。

我的问题是如何为每个函数定义路由,以便我可以通过 url 和 ajax 函数调用这些函数中的每一个

错误

 BookListRazor.Controllers.NewsController.GetSingleNews (BookListRazor)
    BookListRazor.Controllers.NewsController.GetAll (BookListRazor)
    BookListRazor.Controllers.NewsController.GetAllNews (BookListRazor)

代码

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using BookListRazor.Model;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;

    namespace BookListRazor.Controllers
    {
        [Route("api/News")]
        [ApiController]
        public class NewsController : Controller
        {
            private readonly ApplicationDbContext _db;

            public NewsController(ApplicationDbContext db)
            {
                _db = db;
            }

            //get all news by languageID
            [HttpGet]
           // [Route("api/news/getallnews/{1}")]
            public async Task<IActionResult> GetAllNews(int langID)
            {
                //var query = await _db.News.OrderByDescending(x => x.NewsDate).Where(x => x.LanguageID == 1 && x.NewsActive==true && x.NewsVisible==true).ToListAsync();

                return Json(new { data = await _db.News.OrderByDescending(x => x.NewsDate).Where(x => x.LanguageID == langID && x.NewsActive == true && x.NewsVisible == true).ToListAsync() });
            }

            //get all news 
            [HttpGet]
            public async Task<IActionResult> GetAll()
            {
                return Json(new { data = await _db.News.OrderByDescending(x => x.NewsDate).Where(x => x.LanguageID == 1 && x.NewsActive == true && x.NewsVisible == true).ToListAsync() });
            }


            //get single news by newsID
            [HttpGet]
            public async Task<IActionResult> GetSingleNews(int id)
            {
                return Json(new { data = await _db.News.FirstOrDefaultAsync(x=>x.NewsID == id) });
            }

        }
    }

【问题讨论】:

    标签: c# asp.net asp.net-core razor-pages


    【解决方案1】:

    你可以改变如下:

    [Route("api/News/[action]")]
    [ApiController]
    

    请求网址如下:

    //https://localhost:44364/api/news/GetAllNews?langID=1
    [HttpGet]
    public async Task<IActionResult> GetAllNews(int langID)
    {
    }
    
    //https://localhost:44364/api/news/GetAll
    [HttpGet]
    public async Task<IActionResult> GetAll()
    {       
    }
    
    //https://localhost:44364/api/news/GetSingleNews?id=1
    [HttpGet]
    public async Task<IActionResult> GetSingleNews(int id)
    {       
    }
    

    【讨论】:

    • 我在我的代码中做了类似的事情,但忘记删除产生错误[Route("api/news/getallnews/{id}")][Route("api/news/GetAll")][Route("api/news/GetSingleNews/{id}")]的顶级路由
    【解决方案2】:

    可以为每个方法设置[Route]属性,

    这样

        public class TestController : Controller
        {
            [Route("api/Test")]
            public IActionResult Index1()
            {
                return Json("Hello World 1");
            }
    
            [Route("api/Test/Index2")]
            public IActionResult Index2()
            {
                return Json("Hello World 2");
            }
        }
    
    

    请试一试,

    希望对你有帮助

    【讨论】:

    • 感谢您的回复,您的解决方案更符合 MVC,而我专门针对 Razor 页面方法
    猜你喜欢
    • 1970-01-01
    • 2020-02-19
    • 2018-07-28
    • 2019-12-15
    • 2018-10-29
    • 2018-05-09
    • 2018-06-24
    • 2022-07-14
    • 2021-04-28
    相关资源
    最近更新 更多