您可以帮助我在.NET Core 3中创建自定义路由吗?
如果网址看起来像这样:
abc.com/tiger-animal
那么我想在上执行该Index
方法AnimalController
并tiger
作为查询字符串或Id
参数传递。
当url看起来像这样:时abc.com/aboutus
,路由应Index? method on the
按照默认方式执行aboutus`控制器。
这表示我要AnimalController
在网址包含最后一个字符串的情况下执行-animal
。
我试过了
app.UseMvc(routes =>
{
routes.MapRoute("blog", "{*id}-animal",
defaults: new { controller = "animal", action = "index" });
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
但它不起作用。
删除星号,就可以了。
同样值得一提的是,ASP.NET Core 3.0使用UseEndpoints
代替UseMvc
,因此配置路由的正确方法如下:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("blog", "{id}-animal", new { controller = "animal", action = "index" });
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句