This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21930|Reply: 1

[WebAPI] web api Route property definition

[Copy link]
Posted on 4/28/2018 10:37:11 AM | | | |
ASP.NET Web API routing, simply put, is the process of mapping client requests to corresponding Actions. In the article "ASP.NET Web API Practice Series 03, Routing Templates, Routing Conventions, Routing Settings", I experienced setting routes through templates, conventions, and HTTP methods, and the advantage of this approach is that the routing templates are uniformly placed in the WebApiConfig class under the App_Start folder, which is convenient for management, but the disadvantage is that it is not flexible enough.


REST treats everything as a resource, and sometimes, a resource with child resources, such as Customer and Orders, may want to enter a request like customers/1/orders, but it is difficult to achieve this route by convention alone. In fact, ASP.NET Web API provides us with a Route feature, which can be directly connected to Action, which is very flexible and intuitive to use.

Let's experience how to use the Route feature in ASP.NET MVC4.

Allow the Route attribute


First you need to set it up in WebApiConfig.



The above MapHttpAttributeRoutes method is only available in newer versions of ASP.NET Web APIIf your version is relatively low, you can uninstall the old version and install the latest version through the NuGet Package Manager console.


Next, in Global.asax, you need to comment out the original way of registering WebApiConfig and adopt a new method, as follows:

At this time, running the project may report the following error:

This is because when downloading the latest version of ASP.NET Web API, I also downloaded the latest version of icrosoft. AspNet.WebApi.HelpPage。 You can uninstall the latest version of HelpPage and download the older version.

Uninstall-Package Microsoft.AspNet.WebApi.HelpPage –Force
Install-Package Microsoft.AspNet.WebApi.HelpPage -Pre


Use the Route property

Create a Cusomter class.


Create an Order class.

Create a Database class to get the Order collection.



Create an empty API controller written like this:


Type the following in your browser:

If you are using ASP.NET MVC4 for development, you may get the following error when the program is run for the first time:

[A] System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A is derived from "System.Web.WebPages.Razor, version=2.0.0.0, culture=neutral, PublicKeyToken=31bf3856ad364e35" (in "C:/Windows/Microsoft.Net/assembly/GAC_MSIL/ in the context "Default") System.Web.WebPages.Razor/v4.0_2.0.0.0__31bf3856ad364e35/System.Web.WebPages.Razor.dll"). Type B is derived from "System.Web.WebPages.Razor, version=3.0.0.0, culture=neutral, PublicKeyToken=31bf3856ad364e35" (in context "Default" in "C:/Windows/Microsoft.NET/Framework/v4.0.30319/ Temporary ASP.NET Files/vs/feb7ce97/a525d58a/asse


This is because the latest version of Razor is used when downloading the latest version of the ASP.NET Web API. You need to configure the following configuration in the root directory of Web.config:


Use the RoutePrefix feature


If you want to prefix all Actions in an API controller, you can put the RoutePrefix feature on the API controller.

For example, we want it to be in a format like this: http://localhost/api/customers/1/orders


Modify the OrdersController in this way.


You can also use ~ in the Route feature to override the prefix rules of the Action.



The prefix defined by the RoutePrefix feature can also have parameter variables:


Routing constraints

You can constrain parameter variables in a route by "{parameter variable name: constraint}".


Above, if the fragment variable id is of type int, it is routed to the first Action, and if not, it is routed to the second Action.

ASP.NET Web API built-in constraints include:

{x:alpha} constrains uppercase and lowercase letters
{x:bool}
{x:datetime}
{x:decimal}
{x:double}
{x:float}
{x:guid}
{x:int}
{x:length(6)}
{x:length(1,20)} constrains the length range
{x:long}
{x:maxlength(10)}
{x:min(10)}
{x:range(10,50)}
{x:regex(regex)}


You can set multiple constraints for a parameter variable at the same time:

Implement the IHttpRouteConstraint interface to customize the constraint rules. Implement a constraint that cannot be 0.


Register a custom constraint in WebApiConfig in the App_Start folder.


Use custom constraints.


Optional parameters and their default values

If a routing parameter variable is optional, the parameter must also be given a default value.


Add ? after the constraint to indicate optional, and set the default value for id in the method parameters.

Set a name for the route



Routing priority

The route priority set by the Route property is determined based on conventions and the RouteOrder property.

The conventions are:

1. Static fragment variables
2. Fragment variables with constraints
3. Fragment variables without constraints
4. Wildcard fragment variable with constraints
5. Unconstrained wildcard fragment variables

The default value of the RouteOrder property is 0, and the smaller the property value, the higher it is.


Above, the order of priority for routing is:

orders/details static fragment variable, the value of the RouteOrder property is 0
orders/{id} with constrained fragment variables with a value of 0 in the RouteOrder property
orders/{customerName} is a fragment variable without constraints, and the value of the RouteOrder property is 0
orders/{*date} is a wildcard fragment variable with a value of 0 in the RouteOrder property
orders/pending RouteOrder property value of 1






Previous:Keep a record of what can be paid with a credit card
Next:Unable to provide a process for a ADO.NET with the fixed name "MySql.Data.MySqlClient"...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com