Web API 404 错误删除请求

use*_*165 4 asp.net-web-api

我有一个 Web API,它可以完美地处理所有类型的 HTTP 请求(在同一个控制器上),一旦我将它移到生产环境(共享服务器,我什至无法访问它),DELETE请求就停止工作(其他工作正常),我收到 404 错误:

请求的 URL https://www.example.com:443/ Rejected-By-UrlScan~ /API/Users/DeleteUser/1

物理路径 d:\xx\yy\example.com\Rejected-By-UrlScan

匿名登录方法

登录用户匿名

这是 web.config(的一部分):

 <system.web>
    <customErrors mode="Off"/>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
Run Code Online (Sandbox Code Playgroud)

删除操作:

[Authorize]
[RoutePrefix("Users")]
public class UsersController : ApiController
    {
    [HttpDelete]
    [Route("DeleteUser/{id:int}")]
    public void Delete(int id)
    {
        _UsersRepository.Delete(id);
    }
Run Code Online (Sandbox Code Playgroud)

和 AJAX 调用:

deleteUser = function (id, callback) {
    $.ajax({
        url: "../API/Users/DeleteUser/" + id,
        type: "DELETE",
        success: function () {
            callback;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

WebApiConfig.cs:

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();


        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        //create json formatter and remove all others
        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
        config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
        config.Formatters.Remove(config.Formatters.XmlFormatter);
    }
Run Code Online (Sandbox Code Playgroud)

同一控制器上的工作调用示例:

getUsers = function (callback) {
    $.get("../API/Users/GetUsers/", callback);
}
Run Code Online (Sandbox Code Playgroud)

和行动:

[Route("GetUsers")]
public IEnumerable<User> GetUsers()
{
    return _UsersRepository.GetUsers();
}
Run Code Online (Sandbox Code Playgroud)

Sub*_*bbu 5

在您的 IIS 中,您是否配置了 URLScan 扩展?

https://www.iis.net/downloads/microsoft/urlscan

UrlScan 是一种安全工具,用于限制IIS 将处理的 HTTP 请求的类型

URL 中的“Rejected-By-UrlScan”表明扩展程序可能被配置为拒绝“删除”请求。

您可以询问托管 IIS 的服务器的管理员是否在 IIS 中配置了允许删除请求。