"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to handle dot characters (.) in MVC Web API 2 request parameters?

How to handle dot characters (.) in MVC Web API 2 request parameters?

Posted on 2025-04-14
Browse:626

How to Handle Dot Characters (.) in MVC Web API 2 Request Parameters?

Addressing the Dot Character in MVC Web API 2 Request Parameters

In MVC Web API 2, it is convenient to work with request parameters that adhere to a consistent format. However, sometimes it is necessary to handle requests containing special characters, such as a period (.). This article explores the issue of including the dot character in request parameters and offers a practical solution.

Underlying Problem

The user intended to allow requests in the format:

http://somedomain.com/api/people/staff.33311

However, when they tried this, they encountered a 404 error. This occurred because Web API's default routing is designed to recognize segments in the URL path as controller and action names, with optional parameters following them. The dot character in the request URL is causing ambiguity as it is parsed either as a part of the action name or a query parameter.

Proposed Solution

To resolve this issue, the user can ensure that the request parameter in question is not part of a URL path segment it can't be mistaken for a controller or action name. One way to achieve this is by adding a trailing slash to the URL, like this:

http://somedomain.com/api/people/staff.33311/

The trailing slash ensures that the parameter is treated as a query string instead of part of the URL path. This allows Web API to parse the request correctly and map it to the intended action.

Implementation Considerations

Depending on the server configuration, you may also need to adjust web.config to allow dots in the URL path. This can be achieved by setting the "allowDotsInPath" attribute of the httpRuntime element to true, as shown below:


  

By implementing this solution, the user can handle requests with a dot character in the parameter while maintaining a consistent and unambiguous request format.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3