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.

5
  • 1
    This may be of help to you stackoverflow.com/questions/11494200/… Read the section on constraints. Apr 9, 2015 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 9, 2015 at 19:30
  • [Route("~/api/assessments/{id:int}.js")] and your config should have runAllManagedModulesForAllRequests set to true. Apr 9, 2015 at 19:33
  • Well, like I said, I'm trying to emulate the service from RoR... that's what the service is expecting. Apr 9, 2015 at 19:33
  • The route debugger can be really helpful: haacked.com/archive/2008/03/13/url-routing-debugger.aspx I'd suggest posting those results
    – AaronLS
    Apr 9, 2015 at 19:46

1 Answer 1

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>
7
  • It's worth noting that runAllManagedModulesForAllRequests may have performance impacts on your application.
    – David L
    Apr 9, 2015 at 19:35
  • hmm... that does not seem to make a difference. Apr 9, 2015 at 19:36
  • That is very true, it may be a considerable alternative to instead map .js files in IIS to run through ASP.NET modules. Apr 9, 2015 at 19:36
  • Did you replace the existing <modules> line? (or add the runAllManagedModulesForAllRequests="true" property to the tag?) I omitted the closing tag from it in the original answer, but it's not a one-line tag. Apr 9, 2015 at 19:38
  • No I already had a modules tag, so I just added the attribute. Apr 9, 2015 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.