- 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.
- 數據通信網絡實踐:基礎知識與交換機技術
- 網絡協議工程
- 局域網組建、管理與維護項目教程(Windows Server 2003)
- Metasploit Penetration Testing Cookbook
- 網絡安全應急響應技術實戰
- Android UI Design
- Getting Started with Memcached
- 物聯網工程概論
- 5G技術核心與增強:從R15到R17
- 5G非正交多址接入技術:理論、算法與實現
- 5G智慧交通
- Cisco無線局域網配置基礎
- INSTANT Social Media Marketing with HootSuite
- Architecting Data:Intensive Applications
- 物聯網商業設計與案例