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

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.

主站蜘蛛池模板: 蓝山县| 青浦区| 梁河县| 陕西省| 都江堰市| 获嘉县| 祁门县| 游戏| 芒康县| 尖扎县| 韩城市| 安宁市| 宜良县| 迁安市| 深泽县| 云龙县| 剑阁县| 垣曲县| 浮山县| 灵山县| 洪雅县| 咸丰县| 翁源县| 深州市| 左贡县| 临江市| 砚山县| 壤塘县| 宁阳县| 乐都县| 梁山县| 道孚县| 吉林市| 扶绥县| 延津县| 双城市| 如皋市| 无锡市| 吉林省| 怀远县| 溧阳市|