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

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.

主站蜘蛛池模板: 驻马店市| 庆阳市| 长葛市| 丹阳市| 武强县| 丰城市| 珲春市| 海晏县| 开原市| 荥经县| 镶黄旗| 田林县| 长海县| 宜州市| 辽宁省| 霍邱县| 开远市| 大兴区| 莱州市| 万源市| 安阳县| 丹凤县| 达日县| 栾城县| 建平县| 集安市| 永修县| 北碚区| 洪雅县| 易门县| 和田市| 合川市| 怀集县| 米脂县| 龙胜| 依安县| 五河县| 清流县| 泽州县| 衢州市| 唐海县|