[英]Setting up routes in ASP.NET Core 3 MVC web app
I have an ASP.NET Core 3 MVC web app and I have a couple of controllers.我有一个 ASP.NET Core 3 MVC Web 应用程序,并且有几个控制器。
I am trying to form my links to access these controllers and I am unsure what I am doing wrong.我正在尝试形成访问这些控制器的链接,但我不确定我做错了什么。
Controller names: Exhibitors
, DemoQueue
控制器名称:
Exhibitors
, DemoQueue
Each controller has an Index
action which takes 2 int
type parameters每个控制器都有一个
Index
动作,它接受 2 个int
类型参数
public IActionResult Index(int eventId, int companyId)
And here is my relevant Startup.cs
code这是我的相关
Startup.cs
代码
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
So it is my understanding that I can browse to these 2 actions by these urls:所以我的理解是,我可以通过这些 url 浏览到这两个操作:
Exhibitors/1/2
DemoQueue/3/4
But it seems I have to use this longwinded value:但似乎我必须使用这个冗长的值:
Exhibitors/Index?eventId=1&companyId=2
Is there a way to set a route to enable me to go to [controller]/id/id
?有没有办法设置一条路线,使我能够转到
[controller]/id/id
? But go to a different controller eg Exhibitors
or DemoQueue
但是转到不同的控制器,例如
Exhibitors
或DemoQueue
You didn't have your custom route template defined.您没有定义自定义路由模板。 In your
startup.cs
, all you had was the default routing template.在您的
startup.cs
,您拥有的只是默认路由模板。
To map requests like Exhibitors/1/2
and DemoQueue/3/4
to their corresponding controllers Index
method, you need to add the following before the default conventional routing template:要将
Exhibitors/1/2
和DemoQueue/3/4
等请求映射到其对应的控制器Index
方法,您需要在默认的常规路由模板之前添加以下内容:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "exhibitors-custom",
pattern: "exhibitors/{eventId:int}/{companyId:int}",
defaults: new { controller = "exhibitors", action = "index" }
);
endpoints.MapControllerRoute(
name: "demoqueue-custom",
pattern: "demoqueue/{eventId:int}/{companyId:int}",
defaults: new { controller = "demoqueue", action = "index" }
);
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
You can mix the use of conventional routing and attribute routing, but it's typical to use conventional routes for controllers returning HTML for browsers, and attribute routing for controllers serving RESTful APIs.您可以混合使用传统路由和属性路由,但通常将传统路由用于为浏览器返回 HTML 的控制器,并为控制器提供 RESTful API 的属性路由。
Reference: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#mixed-routing-attribute-routing-vs-conventional-routing参考: https : //docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#mixed-routing-attribute-routing-vs-conventional-routing
You can change your code to these您可以将代码更改为这些
public class ExhibitorsController:Controller
[Route("~/Exhibitors/{eventId}/{companyId}")]
public IActionResult Index (int eventId, int companyId)
and
public class DemoQueueController:Controller
[Route("~/DemoQueue/{eventId}/{companyId}")]
public IActionResult Index (int eventId, int companyId)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.