[英]Two Different Routes For One Action in ASP.Net Core 6 MVC
I'm trying doing same action with different routes.我正在尝试用不同的路线做同样的动作。 For example;
例如;
"../City/AddToCity/3 " “../City/AddToCity/3”
"../City/AddToCity?infoId=3 " “../City/AddToCity?infoId=3”
should do same action on frontend.应该在前端做同样的动作。 How can i do that?
我怎样才能做到这一点?
you can use [Route]
attribute.您可以使用
[Route]
属性。
[Route("api/[controller]")]
public class CityController{
[Route("addtocity/{id}")]
[Route("addtocity")]
public IActionResult AddtoCity(int? id = null, int? infoId = null)
{
.....
}
}
Apply the following Route
attribute to the action method:将以下
Route
属性应用于操作方法:
[Route("/City/AddToCity/{infoId?}")]
public IActionResult AddToCity(int? infoId)
{
.... your code
}
But if the City
is your controller name, the AddToCity
is the action method name and you have the following default route mapping in the Startup.cs
但是如果
City
是您的 controller 名称,则AddToCity
是操作方法名称,并且您在Startup.cs
中有以下默认路由映射
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
then you don't need to apply any additional routes.那么您不需要应用任何其他路线。 Just change the parameter name to
id
:只需将参数名称更改为
id
:
public IActionResult AddToCity(int? Id)
{
.... your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.