- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 142字
- 2021-06-18 18:35:58
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.
推薦閱讀
- Implementing VMware Horizon 7(Second Edition)
- Computer Vision for the Web
- 算法基礎:打開程序設計之門
- Flask Web開發入門、進階與實戰
- Android 7編程入門經典:使用Android Studio 2(第4版)
- Java程序員面試算法寶典
- Visual Basic程序設計實驗指導(第4版)
- Unity 2018 Augmented Reality Projects
- Xcode 6 Essentials
- Learning VMware vSphere
- C++程序設計教程(第2版)
- Learning Splunk Web Framework
- Java7程序設計入門經典
- Visual C++程序設計與項目實踐
- Mastering Object:Oriented Python(Second Edition)