Problem:
Attempting to access resources using URLs containing special characters, such as dots (`.') in the path segment, results in a "HTTP Error 404.0 - Not Found" error.
Configuration:
The sample controller and WebApiConfig provided in the question:
// PeopleController.cs public class PeopleController : ApiController { // ... } // WebApiConfig.cs public static void Register(HttpConfiguration config) { // ... config.Routes.MapHttpRoute(name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
Solution:
To allow special characters in the path segment, such as dots, modify the WebApiConfig route as follows:
config.Routes.MapHttpRoute(name: "DefaultApi", routeTemplate: "api/{controller}/{id}/{*path}", defaults: new { id = RouteParameter.Optional, path = RouteParameter.Optional } );
This route template adds a catch-all route parameter named {*path} that captures any remaining path segments, including special characters like dots.
Example:
http://somedomain.com/api/people/staff.33311/
By appending a slash (/) to the end of the URL, the request can be routed to the controller action that handles the path segment, in this case staff.33311.
The above is the detailed content of How to Handle URLs with Special Characters in ASP.NET Web API 2 Routing?. For more information, please follow other related articles on the PHP Chinese website!