【发布时间】:2019-08-05 08:20:42
【问题描述】:
我想知道如何使用带有前缀路由路径的单个可选参数设置 url 路径。 因为当我尝试做 [HttpGet("id:int?}"] 时,我尝试访问 “[我的服务器]/前缀” 它不工作。
我这样做是为了 [我的服务器]/前缀 [myserver]/prefix/1
这是路线设置 字首 [路线(“管理/测试”)] 功能 [HttpGet("{id:int?}")
我知道它正在使用其他路由设置 -> [HttpGet("")] 或者 设置前缀路由 [路线(“管理”)] 功能路线 [HttpGet("test/{id:int?}"]
但我想知道为什么第一个参数是路由的唯一一个词并且它不起作用。
[Route("manage/test")]
public class ManageController : BaseController //(inherited from Controller)
{
//[HttpGet("")]
[HttpGet("{id:int?}"]
public IActionResult Index(int? id = null)
{
}
}
对于这个“[myserver]/prefix”
未找到视图“/Views/manage/test/.cshtml”。搜索了以下位置:/Views/manage/test/.cshtml
但是对于“[myserver]/prefix/1”
未找到视图“/Views/manage/test/.cshtml”。搜索了以下位置:/Views/manage/test/Index.cshtml
[Route("manage")]
public class ManageController
{
[HttpGet("{id:int?}"]
public IActionResult Index(int? id = null)
{
return View();
}
}
【问题讨论】:
-
Index的内容是什么?对于 View not found 错误,它与Index中的返回有关,而不是与路由有关。与我们分享重现问题的详细步骤。 -
谢谢回答,这只是举例。所以在带有“return view()”的真实代码中
-
我的意思是,当我刚刚写 [HttpGet("{id:int?"}")] 时,url 管理/测试不起作用
-
是否有任何演示可以重现您的问题?
-
为此我需要添加 [HttpGet("")]。但正如你所知,{id?} 表示 id 是可选参数。但它不能作为没有任何预附加路径的唯一参数。
标签: asp.net-core controller routing http-get