【发布时间】:2020-01-25 08:21:10
【问题描述】:
- 使用“UseMvc”路由但无法调用控制器
- 在启动页面中添加了
service.AddMvc方法&在配置部分它是app.useMvc() - 我无法路由,无法弄清楚问题所在
控制器代码在这里并且有路由:动作方法是Get,参数startDateTime类型
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();
}
}
【问题讨论】:
-
@Priyanka 我应该更改我的答案以匹配您的最新编辑吗?
-
@AbdulG 是的,因为应用程序版本是 ASP.NET core 2.2 并且不包含 EnableEndpointRouting 的定义。
-
已编辑答案,立即尝试。
-
不要在
Configure方法内添加路由到路由表
标签: asp.net-core asp.net-core-mvc asp.net-core-webapi