- Hands-On Full:Stack Web Development with ASP.NET Core
- Tamir Dresher Amir Zuker Shay Friedman
- 583字
- 2021-06-10 19:37:29
Defining a new route template
To define route templates in your application, the easiest way is to use the UseMvc method, instead of UseMvcWithDefaultRoute inside the Configure method in your startup class. This method allows you to define the routes you want in your application. For example, in the GiveNTake application, if we want our application to support not only the default route, but also expose the RESTful API with an api prefix (that is, URLs in the form of /api/[controller]/[action]), then this is how we need to change our Configure method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseMvc(routes =>
{
routes
.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}")
.MapRoute(name: "api", template: "api/{controller}/{action}/{id?}");
});
}
The MapRoute method is how you can define routes for your application, and you can call it multiple times to set multiple route templates.
To test the route you just configured, add a new empty class in the Controllers folder and name it MessagesController. Paste the following code to the file you created. It should look something like the following:
using Microsoft.AspNetCore.Mvc;
namespace GiveNTake.Controllers
{
public class MessagesController : Controller
{
public string[] My()
{
return new[]
{
"Is the Microwave working?",
"Where can i pick the washing machine from?",
};
}
public string Details(int id)
{
return $"{id} - Is the Microwave working?";
}
}
}
Run the project and navigate your browser to http://localhost:{port}/api/messages/my.
Your browser should display a page similar to this:

The MapRoute methods allow you to control the route in a more advanced way. Here is the full MapRoute signature, but it has overloads that make some of the parameters optional:
IRouteBuilder MapRoute(this IRouteBuilder routeBuilder,
string name,
string template,
object defaults,
object constraints,
object dataTokens)
These parameters operate as follows:
- name: Each route should be given a unique name that identifies it. The name doesn't affect the routing procedure, but it can be very useful when there are route failures, and ASP.NET Core notifies you on issues with the routes.
- template: This is the core of the route. This defines the URL structure and the tokens that should be mapped to the controller, actions, and parameters.
- defaults: This defines the default values for the different tokens in case they are missing in the request URL.
- constraints: This parameter covers individual constraint rules for the tokens in the route that determine if the value is acceptable for that token in that route.
- data token: These are additional values that are associated with the route. They won't affect the matching process, but when the route is determined, the values will be added to the RouteData.DataTokens collection property of the controller and can be used in its logic.
Here is an improved Version of our API route definition that sets the default value for the controller to Messages, the action to My, and also sets a constraint on the id parameter to allow only integers:
MapRoute(
name: "api",
template: "api/{controller}/{action}/{id?}",
defaults: new { Controller = "Messages", action="My" },
constraints: new { id = new IntRouteConstraint() });
defaults and constraints can also be set inside the template itself, so the preceding API definition we created is identical to this one:
MapRoute(
name: "api",
template: "api/{controller=Messages}/{action=My}/{id:int?}");
Conventional routing is a simple mechanism that is usually suited to small-scale APIs. But as your APIs grow, you will soon find that you need a more fine-grained approach for defining your routes, and if that's the case, it's better to use attribute-based routing.
- 高校網絡道德教育研究
- 智能網聯汽車V2X與智能網聯設施I2X
- 數字烏托邦
- 萬物互聯:蜂窩物聯網組網技術詳解
- Oracle SOA Suite 11g Performance Tuning Cookbook
- 計算機網絡工程實用教程(第2版)
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- 面向5G-Advanced的關鍵技術
- jQuery Mobile Web Development Essentials
- 網管第一課:網絡操作系統與配置管理
- 端到端QoS網絡設計
- Learning Windows 8 Game Development
- 互聯網安全的40個智慧洞見(2016)
- 基于IPv6的家居物聯網開發與應用技術
- 物聯網與智慧廣電