ASP.NET核心路由不起作用 [英] ASP.NET Core Route not working
问题描述
路由器的配置方式如下:
The Routers are configured this way:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "api",
template: "api/{action}/{id?}");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "spa-fallback",
template: "{*url}",
defaults: new { controller = "Home", action = "Index"});
});
我要请求的控制器动作如下所示: //GET api/values/5
The controller I action I am trying to request looks like this: // GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value" + id;
}
当我请求 http://localhost:54057/api/values/get 时,我取回"value0".
When I request http://localhost:54057/api/values/get, I get back "value0".
当我请求 http://localhost:54057/api/values/get 时,我取回"value0".
When I request http://localhost:54057/api/values/get, I get back "value0".
当我请求 http://localhost:54057/api/values/get/5 ,我找回404未找到.
When I request http://localhost:54057/api/values/get/5, I get back a 404 Not Found.
我的路由配置不正确,还是为什么没有将"id"参数从URL传递到控制器操作?
Are my routes configured incorrectly, or why is that that the "id" parameter is not passed from the URL to the controller action?
推荐答案
定义完整路由,SPA会覆盖默认的MVC路由
Define the full route, SPA overrides the default MVC routing
[HttpGet("api/[controller]/[action]/{id}")]
public IActionResult Get(int id)
{
return ...;
}
这篇关于ASP.NET核心路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!