我有asp.net核心应用程序。在我的应用程序中,默认路由设置为Home/Index/id
我还有一个控制器名称Document
public class DocumentController : Controller
{
public IActionResult Index(int? id)
{
// id may be null. Do something with id here
return View();
}
public IActionResult Detail(int id)
{
return View();
}
}
在startp.cs中我已经设置了如下路线
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "document",
template: "Document/{action}/{id?}",
defaults: new { controller = "Document", action = "Index" });
});
我可以将文档视图浏览为http://localhost:40004/Document
或http://localhost:40004/Document?id=1234
但是我希望以下路线有效
http://localhost:40004/Document/1234
http://localhost:40004/Document?id=1234
http://localhost:40004/Document/Details/1234
http://localhost:40004/Document/Details?id=1234
startup.cs中的路由配置应该是什么?
答案 0 :(得分:1)
编辑:根据问题编辑
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "document",
template: "Document/{id?}",
defaults: new { controller = "Document", action = "Index" });
});
此路线定义适用于您拥有的所有路线模式
您不需要详细URL的特定路由定义,因为它与默认约定匹配(由默认路由定义处理)。所以它会正常工作