官术网_书友最值得收藏!

Setting route defaults

We've seen how we can specify default values for route parameters in the template, but there's also another way: by overloading the MapControllerRoute extension method that takes an object containing default values. Instead of supplying these defaults as strings, you can have this:

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

This is valid even if you don't have the tokens in the route, as follows:

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "My/Route",
defaults: new { controller = "My", action = "Route" });
});

Remember that you do have to supply controller and action; if they are not present in the template, they need to be supplied as defaults.

The next section delves into the inner workings of routes and how we can work around with requests.

主站蜘蛛池模板: 黄大仙区| 漳平市| 洪湖市| 聂荣县| 宜川县| 当涂县| 樟树市| 铜陵市| 施甸县| 北宁市| 南充市| 景宁| 集贤县| 龙州县| 巴里| 青州市| 海兴县| 南靖县| 富源县| 青阳县| 晋江市| 元氏县| 岳西县| 武清区| 龙里县| 商都县| 巴楚县| 卢湾区| 梨树县| 栾城县| 邹城市| 沭阳县| 务川| 那曲县| 伊宁县| 兴国县| 唐河县| 武功县| 文化| 高邑县| 崇阳县|