2

OK it's not that funny an ending...

I am trying to emulate a RoR-based service in .Net WebAPI. The Ruby implementation of the service is supposed to return a JSON document from a url of:

http://myserver/api/assessments/{id}.js

Note the .js at the end.

I made a RouteAttribute decoration on my api controller like so:

[Route("~/api/assessments/{id}.js")]
public async Task<HttpResponseMessage> GetAssessment(int id)
{
    . . .
}

...but I'm getting a 404 error. I suspected this might be because the request ended in "js", so after a bit of research I found I should set my RouteCollection.RouteExistingFiles to true... this did not seem to have any effect. I am still getting 404.

Am I right? is the .js ending what is causing the 404? How can I get around this? This is a pure WebApi project, so it's not like I'm using JavaScript in it anyway.

AaronLS
  • 37,329
  • 20
  • 143
  • 202
Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • 1
    This may be of help to you http://stackoverflow.com/questions/11494200/can-periods-be-used-in-asp-net-web-api-routes Read the section on constraints. – Praveen Paulose Apr 09 '15 at 19:26
  • Is there any reason why you have to keep this "extension" on your service endpoint? This should never be necessary in Web API since you can content-negotiate to return JSON. – David L Apr 09 '15 at 19:30
  • [Route("~/api/assessments/{id:int}.js")] and your config should have runAllManagedModulesForAllRequests set to true. – Praveen Paulose Apr 09 '15 at 19:33
  • Well, like I said, I'm trying to emulate the service from RoR... that's what the service is expecting. – Jeremy Holovacs Apr 09 '15 at 19:33
  • The route debugger can be really helpful: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx/ I'd suggest posting those results – AaronLS Apr 09 '15 at 19:46

1 Answers1

3

Do you have the following in your Web.config? I recently just setup Routing for a project for Work and this line caused me all sorts of hell because it was not present:

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

The .js extension may not be getting picked up by the managed modules that do the routing.

Addendum: Additional data that is within that configuration section and required to make it work:

<configuration>
  ...
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule"
         type="System.Web.Routing.UrlRoutingModule, 
               System.Web.Routing, Version=3.5.0.0, 
               Culture=neutral, 
               PublicKeyToken=31BF3856AD364E35" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler"
        preCondition="integratedMode"
        verb="*" path="UrlRouting.axd"
        type="System.Web.HttpForbiddenHandler, 
              System.Web, Version=2.0.0.0, Culture=neutral, 
              PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>
    ...
  </system.webServer>
</configuration>
Der Kommissar
  • 5,848
  • 1
  • 29
  • 43