Asp.net core ASP.NET核心Web API路由 使用“UseMvc”路由,但无法调用控制器 在启动页面中添加了service.AddMvcmethod&在配置部分添加了app.useMvc() 我无法确定路线,也无法找出问题所在

Asp.net core ASP.NET核心Web API路由 使用“UseMvc”路由,但无法调用控制器 在启动页面中添加了service.AddMvcmethod&在配置部分添加了app.useMvc() 我无法确定路线,也无法找出问题所在,asp.net-core,asp.net-core-mvc,asp.net-core-webapi,Asp.net Core,Asp.net Core Mvc,Asp.net Core Webapi,控制器代码在这里,并且具有路径:操作方法为Get,参数为DateTime类型的start public void ConfigureServices(IServiceCollection services) { services.AddDbContext<CurrencyContext>(cfg => { cfg.UseSqlServer(_config.GetConnectionString("BitCoinIndexConnect

控制器代码在这里,并且具有路径:操作方法为
Get
,参数为
DateTime
类型的
start

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<CurrencyContext>(cfg => {
                  cfg.UseSqlServer(_config.GetConnectionString("BitCoinIndexConnectionString"));
    });

    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();
    app.UseNodeModules(env);

    app.UseMvc(routes =>
                      {             
                          routes.MapRoute(name: "default",
                                          template: "api/{controller}/{action}/{start:DateTime}",
                                          defaults: new { 
                                                            controller = "Currency", 
                                                            action = "Get", 
                                                            start = DateTime.Now.AddDays(-14)});                          
                                                        });
                      }
}

[Route("api/[Controller]")]
public class CurrencyController : Controller
{
    private BitCoinRepository<BitCoinIndex> _repository;

    public CurrencyController(BitCoinRepository<BitCoinIndex> repository)
    {
        _repository = repository;
    }

    // GET: api/<controller>
    [HttpGet("{start}",Name ="Get")]
    public IActionResult Get(DateTime start)
    {
        // var bci =  _repository.GetByDates(start).ToDictionary(t => t.Date.ToString(), t => t.Rate);
        return View();    
    }
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(cfg=>{
UseSqlServer(_config.GetConnectionString(“BitCoinIndexConnectionString”);
});
services.AddMvc();
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseStaticFiles();
app.UseNodeModules(env);
app.UseMvc(路由=>
{             
routes.MapRoute(名称:“默认”,
模板:“api/{controller}/{action}/{start:DateTime}”,
默认值:新{
controller=“货币”,
action=“Get”,
start=DateTime.Now.AddDays(-14)});
});
}
}
[路由(“api/[控制器]”)]
公共类CurrencyController:控制器
{
私有比特币存储库(bitconrepository);;
公共货币控制器(比特币存储库)
{
_存储库=存储库;
}
//获取:api/
[HttpGet(“{start}”,Name=“Get”)]
公共IActionResult Get(日期时间开始)
{
//var bci=_repository.GetByDates(start).ToDictionary(t=>t.Date.ToString(),t=>t.Rate);
返回视图();
}
}

我面临同样的问题,并使用属性路由解决了它。这就是我所做的。如果您没有使用.Net Core 3,请忽略第1点

首先,通过在
ConfigureServices
中添加此项禁用端点路由:

services.AddMvc(options => options.EnableEndpointRouting = false);
现在,您可以在
配置
方法中使用此选项

app.UseMvc();
接下来,只需在控制器内定义路由(请记住,我通常更喜欢通过将路由添加到路由表来进行路由,但在执行此“路由”时遇到不必要的问题,属性路由是最容易执行的“路由”)

[路由(“api/myctrl”)]
[ApiController]
公共类MyControllerController:ControllerBase
{
[HttpGet]
[路线(“获取某物”)]
公共异步任务DoStuff()
{
}
}

通过使用
@Url.Action(“DoStuff”、“MyController”)访问此文件
/api/myctrl/getsomething

首先阅读此文,@Priyanka我应该更改我的答案以匹配您的最新编辑吗?@AbdulG是的,因为应用程序版本是ASP.NET core 2.2&不包含EnableEndpointRouting的定义。编辑了答案,请立即尝试。不要将路由添加到
Configure
method中的路由表应用程序是ASP.NET CORE 2.2&不包含Mvc选项的定义。EnableEndpointRoutingYes,它来自.NET CORE 3。没问题,在这种情况下,忽略这一点。
[Route("api/myctrl")]
[ApiController]
public class MyControllerController : ControllerBase
{
    [HttpGet]
    [Route("getsomething")]
    public async Task<JsonResult> DoStuff()
    {

    }

}