ServiceStack Backbone.Todos Delete 405 not allowed

2019-02-17 06:26发布

问题:

I realized when click Backbone.Todos example "Clear x completed items" I get a DELETE 405 not allowed...

I understand from the pervious helps and docs that if I want to enable DELETE PUT PATCH ... I need to set

X-http-method-override : DELETE

if it was a form or in jquery.

But I am not sure how this is done in the Backbone.Todos example as I am new to backbone.js.

Could you please point out how to fix the DELETE 405 ? Thank you.

EDIT ---------------------------------------

I can always change routes to ...

[Route("/todos/add")] //C - post
[Route("/todos/{id}")] //R - get
[Route("/todos/{id}/edit")] //U - post
[Route("/todos/{id}/delete")] //D - post

So, only Post and Get are enough to do the job. But it doesn't look very Restful compare to:

[Route("/todos/{id}", "Delete")] //D - delete

Does it?

回答1:

Backbone.js has special support for this, which you can enable with:

Backbone.emulateHTTP = true

From their website:

emulateHTTP Backbone.emulateHTTP = true

If you want to work with a legacy web server that doesn't support Backbones's default REST/HTTP approach, you may choose to turn on Backbone.emulateHTTP. Setting this option will fake PUT and DELETE requests with a HTTP POST, setting the X-HTTP-Method-Override header with the true method. If emulateJSON is also on, the true method will be passed as an additional _method parameter.

Backbone.emulateHTTP = true;

model.save();  // POST to "/collection/id", with "_method=PUT" + header.

The 405 response may be the result of having something else running in IIS like WebDav that will hijack the and reject the request before it reaches ServiceStack. Otherwise if it's being rejected on the client you may want to enable CORS to allow additional HTTP Verbs to be sent.



回答2:

After some digging, I realized it is not the Backbone setting (which is Backbone.emulateHTTP = false; in line #34 of backbone.js by the way)

I checked Fiddler and it says:

Module  WebDAVModule
Notification  MapRequestHandler
Handler  ServiceStack.Factory
Error Code  0x00000000

It was the WebDAV hijacking and rejecting the request as mythz suspected. Then I found out how to disable WebDAV from the web.config:

  ...
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true"/>
    </handlers>
  </system.webServer>
  ...

Now it works happily. Thank you!