代码之家  ›  专栏  ›  技术社区  ›  si2030

Asp。net Core 3.0端点路由与httpget并接收查询字符串

  •  0
  • si2030  · 技术社区  · 6 年前

    所以我觉得这很容易。。啊。

    我怀疑它与2.2没有什么不同,但由于某种原因,我无法获得匹配的路由并接受查询字符串。我不知道这是否与新的端点路由等有关,所以为了他人的利益,当然也为了我自己的利益,我提出了一个问题,尽管这应该很简单。

    我确实看过 this 问题,虽然它与id类似。我有一个查询,从另一端的javascript程序返回的表单格式如下:

    http://localhost:57000/api/Client/Index?$top=10&$orderby=ClientNo%20asc,ClientLastName%20asc,MobilePhone%20asc
    

    我得到了以下信息:

    aurelia-fetch-client.js:199 GET http://localhost:57000/api/Client/Index?$top=10&$orderby=ClientNo%20asc,ClientLastName%20asc,MobilePhone%20asc 404 (Not Found)
    

    我的创业公司。政务司司长如下:

            app.UseRouting();
            app.UseEndpoints(endpoints => {
                endpoints.MapControllers();
    
            app.UseAuthentication();
            app.UseStaticFiles();
    
            app.UseHttpsRedirection();
            app.UseFileServer();
    
            app.UseSession();
            app.UseAuthorization();
            app.UseCors();
            app.UseConfigureSession();
    

    所以我用了“UseEndpoints”而不是“app.UseMvc…”我不确定这是否会改变我构建控制器的方式。

    我的控制器具有以下功能:

    [Microsoft.AspNetCore.Components.Route("api/[controller]")]
    public class ClientController : Controller {
    

    以及控制器方法:

        [HttpGet(template: "index{query}", Name = "ClientIndex1")]
        public IActionResult Index(string query) {
            var clientList = _clientServices.GetClients();
    
            return new OkObjectResult(clientList);
        }
    

    那么,我在这个问题上做错了什么?还有其他问题吗?

    0 回复  |  直到 6 年前
        1
  •  0
  •   verbedr    6 年前

    我认为问题在于路由模板“index{query}”。Http请求url为 " http://localhost:57000/api/Client/Index “在?之后的所有内容都被解析为参数。

    我会写这样的东西

    [HttpGet(template: "index", Name = "ClientIndex1")]
    public IActionResult Index() {
        var clientList = _clientServices.GetClients();
        // use Request.Query["$top"] to access the odata parameters.
        return new OkObjectResult(clientList);
    }
    
    推荐文章