【问题标题】:Asp.net Core 3.0 Endpoint routing with httpget and receiving a query string使用 httpget 的 Asp.net Core 3.0 端点路由并接收查询字符串
【发布时间】:2019-08-10 02:39:37
【问题描述】:

所以我认为这很容易......啊。

我怀疑它与 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)

我的startup.cs如下:

        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);
    }

那么我在查询中做错了什么,还有其他问题吗?

【问题讨论】:

    标签: asp.net-core asp.net-web-api


    【解决方案1】:

    我认为问题在于您的路由模板“index{query}”。 Http请求网址是 "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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2017-09-09
      • 2010-11-01
      • 1970-01-01
      相关资源
      最近更新 更多