1

IIS 10.0, VS 2022, .NET Framework 4.7.2 Web API

I inherited a web api application that I am told worked 100% the last time it was published (at least a year ago, maybe 2). However, now I get 404 not found when the attribute contains a period (both in IDE using IIS and when published to IIS on server).

I tried creating a new web api app with 2 methods:

    [HttpPost]
    [Route("BatchData/ProcessResponseByString/{requestId}")]

    public HttpResponseMessage ProcessResponseByString(string requestId)
    {
        return new HttpResponseMessage()
        {
            Content = new StringContent($"String:{requestId}")
        };
    }

    [HttpPost]
    [Route("BatchData/ProcessResponseByDouble/{requestId:double}")]

    public HttpResponseMessage ProcessResponse(double requestId)
    {
        return new HttpResponseMessage()
        {
            Content = new StringContent($"Double:{requestId}")
        };
    }

These urls work: https://localhost:44352/BatchData/ProcessResponseByString/12345 https://localhost:44352/BatchData/ProcessResponseByDouble/12345

These urls fail with 404 not found: https://localhost:44352/BatchData/ProcessResponseByString/12345.67 https://localhost:44352/BatchData/ProcessResponseByDouble/12345.67

Perhaps an IIS upgrade changed the way routing is resolved in IIS?

Any ideas on how to resolve this?

Thanks.

  • Can you try to add a / at the end of the urls that are not working, maybe .67 is causing it to think it's an extension rather than a double, so end with /12345.67/. also see https://stackoverflow.com/questions/21387974/why-is-my-web-api-method-with-double-args-not-getting-called/21388760#21388760 – Paritosh Jan 16 '23 at 14:52
  • Thanks Paritosh. that does work if I manually add the slash, but not in swagger and I need it to work in swagger. Does seem to be a kind of 'hack' to me - I would think there would be an MS/ASP.NET solution as it seems many others were having the same issue. – Douglas Marquardt Jan 16 '23 at 15:36
  • 2
    Hi @Paritosh. I read further down in the link and found a web config solution that did work: `code` `code` – Douglas Marquardt Jan 16 '23 at 15:51
  • Hi sir, it looks like your problem has been solved, isn't it? – YurongDai Jan 17 '23 at 02:49

2 Answers2

0

@Paritosh posted the link (Why is my Web API method with double args not getting called?) and suggested the / workaround (thanks for the link), but the answer that worked for me was after the / suggestion - updating webconfig

0

For ASP.NET/IIS, Suprotim Agarwal provides web.config rewrite code here: https://www.devcurry.com/2015/07/allowing-dots-in-aspnet-mvc-paths.html - this rewrite interactively adds a slash to the end of the URL (that contains a period) when it is not a request for a directory or file:

<system.webServer>

    <rewrite>
        <rules>
            <clear />
            <rule name="AddTrailingSlashRule1" stopProcessing="true">
                <match url="(.*[^/])$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Redirect" url="{R:1}/" />
            </rule>
        </rules>
    </rewrite>

</system.webServer>

EDIT 2023-03-17: Unfortunately I had to abandon this approach because when I used this, my Angular app would produce the following error:

Failed to load module script: Expected a Javascript module script but the server responded with a MIME type of "text/html".  Strict MIME type checking is enforced for module scripts per HTML spec.

It was hard to be sure this rewrite was causing the error because when I removed it from web.config the error persisted, even if I recycled my IIS App Pool. Something persists on a per-browser basis - if I started up a different browser the state of web.config (no-longer having the rewrite) would mean that app worked again.

StackOverflowUser
  • 945
  • 12
  • 10