简体   繁体   English

Net 6 中的路由,如何允许 url {controller}/{action}/{id}/{string}

[英]Routage in Net 6, how to allow url {controller}/{action}/{id}/{string}

in the program.cs i found this line of code:在 program.cs 中,我找到了这行代码:

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

but i am not sure how to do in order to allow my website to have such a URL: www.domain.com/mycontroller/id/title但我不知道该怎么做才能让我的网站有这样的 URL: www.domain.com/mycontroller/id/title

Could you help me to make this happen?你能帮我实现这个目标吗? what do i need to change?我需要改变什么? and in the controller i need to add something?在 controller 中我需要添加一些东西吗? and the link asp-action how should be?和链接 asp-action 应该如何? Thanks谢谢

If you want to allow url {controller}/{action}/{id}/{string} in the controller,you can use like this:如果你想在 controller 中允许 url {controller}/{action}/{id}/{string} ,你可以这样使用:

[Route("[controller]/[action]")]
public class TestController : Controller
{
   [Route("{id}/{s}")]
   public IActionResut Test(string id,string s)
   {
       return View();
   }

}

If you want to use <a> tag:如果你想使用<a>标签:

<a asp-action="AddStudy" asp-route-id="xxx" asp-route-s="xxx">a</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM