[英]How to create “/<Controller>/{id1}/<Action>/{id2}” such route in razor pages?
I have a requirement where I have to create a route like /Class/10/Student/134
to get an information of a student whose role id is 134 and whose class id is 10. How can I create such a route in Razor pages? 我有一个必须创建
/Class/10/Student/134
的路由的信息,以获取角色ID为134,班级ID为10的学生的信息。如何在Razor页面中创建这样的路由?
I have added the following route in Startup.cs 我在Startup.cs中添加了以下路由
routes.MapRoute(
name: "GetStudentFromClass",
template: "Class/{classId}/Student/{studentId?}",
defaults: new
{
controller = "Class",
action = "Student"
}
);
and my pages directory structure is like 我的页面目录结构就像
- /Pages
- /Class
- Student.cshtml
- Student.cshtml.cs
In Student.cshtml, I have added this @page "{classId:int}/Student/{studentId:int?}"
to first line 在Student.cshtml中,我已将此
@page "{classId:int}/Student/{studentId:int?}"
到第一行
But it is not working. 但这是行不通的。 Any suggestions, where I did wrong?
有什么建议,我做错了什么?
The mapRoute
method is for defining MVC routes. mapRoute
方法用于定义MVC路由。 Razor Pages routing is handled a little differently. Razor Pages路由的处理方式略有不同。 You should only need to define a template as part of the
@page
directive. 您只需要将模板定义为
@page
指令的一部分。 In this case, it should be: 在这种情况下,应为:
@page "/Class/{classid:int}/Student/{studentid:int}"
There is no need to add anything to the Startup file. 无需向启动文件添加任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.