[Boiled ASP. NET Web API2 methodology] (3-4) to set routing options

Source: Internet
Author: User

Problem

How to create a route, regardless of whether the client passes this parameter, can be successfully matched.

Solution Solutions

Both centralized Routing and attribute routing for ASP. NET WEB APIs support routing declaration optional Parameters.

An optional parameter can be specified by routeparameter.optional in a centralized route, and routeparameter.optional is equivalent to urlparameter.option in MVC.

attribute routing, by adding a greeting as a suffix after an optional parameter. At the same time, you must specify a default value for it

Working principle

Fundamentally, the optional parameter routing supported by the ASP. is actually a variant of the default value route. Routeparamter is a mutable type in code snippet 3-8. This example is intended to be a comparison to illustrate that the route optional parameter is a variant of the route default value.

In attribute routing, add a question mark suffix to the route parameter to Routeentry, using the default value of Routeparameter.optional.

At run time, when the framework processes HTTP requests, Httproutingdispatcher removes them if the optional parameters do not have values. He is able to support/resource and/resource/{optionalparameter}.

Code Demo

As shown in code snippet 3-9, the attribute route used defines the route optional parameter, which is the basic paging that allows the client to pass parameters.

The same route, we can use centralized routing as shown in code snippet 3-10 to define route optional parameters.

[Boiled ASP. NET Web API2 methodology] (3-4) to set routing options

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.