0

I have the following action code to accept a service request:

namespace MyStuff.api
{
    [RoutePrefix("api/Dy")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [Authorize]
    public class DyController : ApiController
    {
        //...
        [Route("{pit}/{name}")]
        public void Post(string pit, string name, [FromBody]JObject obj)
        {
            //...
            #region do something
            // work with name here
            #endregion
        }
        //...
    }
}
Test Scenario

I'm in the debugger and a breakpoint is set somewhere inside the Post method. In the data set I found a brand name "3.1 Phillip Lim". I try to figure out how I can manipulate this name (and other similarly creative names) so that the Post method gets hit.

Breakpoint not hit - request does not arrive in the Post method
- $.post("http://undisclosed.server.net/api/Dy/Brands/3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
- $.post("http://undisclosed.server.net/api/Dy/Brands/-3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
- $.post("http://undisclosed.server.net/api/Dy/Brands/id-3.1%20Phillip%20Lim", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
- $.post("http://undisclosed.server.net/api/Dy/Brands/body.Name", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
- $.post("http://undisclosed.server.net/api/Dy/Brands/{body.Name}", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
Breakpoint hit - request arrives as expected
$.post("http://undisclosed.server.net/api/Dy/Brands/body-Name", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })
$.post("http://undisclosed.server.net/api/Dy/Brands/{null}", MyStuff.Brands[30], function (result) { MyStuff.BrandsResult = result; })

WebApi 2 obviously does something behind the scenes - might be some kind of type recognition for the segments in the URL...

Please explain what is going on ... or how you worked around this. Thanks.

dr. rAI
  • 1,773
  • 1
  • 12
  • 15

1 Answers1

0

Seems to me that your issue is directly related to the dot character inside your route parameter. This is a known issue and depends on the fact that IIS/Web API interprets your query like a static file request (because it ends with a dot + something that seems like an extension).

What you can try:

  • Add a trailing slash to your requests URI:

    http://undisclosed.server.net/api/Dy/Brands/3.1%20Phillip%20Lim/.

    This should be enough to allow Web API to handle the request but I must admit that this is more of a workaround. You may create a rewrite in IIS to make this the default behavior as per this question.

  • Add RAMMFAR to your Web.config file:

    <configuration>
        <system.webServer>
            <modules runAllManagedModulesForAllRequests="true" />
        </system.webServer>
    </configuration>
    

    This could be enough, but if it does not work alone then use it in conjunction with below methods. Also be aware of the performance implications when using RAMMFAR.

  • Add an ExtensionlessUrlHandler for any path (*) to your Web.config:

    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0"  path="*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    
  • Simply give up and never use dots in trailing route parameters. Usually I try to move parameters that may contains URI unfriendly characters (like dots, spaces, and any character that must be encoded) into the query string. Another option is to simply replace those characters with more URI-friendly ones (e.g. dashes).

Community
  • 1
  • 1
Federico Dipuma
  • 17,655
  • 4
  • 39
  • 56