在IIS上PUT请求.NET API返回405方法不允许

时间:2013-12-28 16:16:59

标签: api iis put http-status-code-405

我已经完成了之前的所有帖子。所有提到WebDAV的问题。我没有在我的IIS上安装WebDAV,并且在我的web.config的system.webserver部分添加了删除措施:

<system.webServer>
<modules>
  <remove name="WebDAVModule" />
</modules>
<handlers>
  <remove name="WebDAV" />
  <add name="WebDAV" path="*" verb="*" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    <add name="Access-Control-Allow-Headers" value="Authorization, Content-Type" />
  </customHeaders>
</httpProtocol>

但是在使用PUT发送和更新到API时,我仍然收到405(Method Not Allowed)错误。

还有其他原因会发生这种情况吗?

修改

事实证明,PUT请求在同一域的其他控制器上是正常的:

即。对/ api / crates的PUT工作正常,但对api / run的PUT没有。在本地控制器级别会出现什么问题?

CONTROLLER

//[Authorize]
public class RunsController : ApiController
{
    // GET api/values
    public IEnumerable<Run> Get()
    {
        List<Run> runs = new List<Run>();

        // get tasks from DB 
        using (SqlConnection runConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            SqlCommand runCommand = new SqlCommand("ct_runs", runConn);
            runCommand.CommandType = CommandType.StoredProcedure;
            runConn.Open();
            SqlDataReader runReader = runCommand.ExecuteReader();
            while (runReader.Read())
            {
                // Add items to task list
                long thisId;
                if (runReader["Id"] != DBNull.Value) { thisId = Convert.ToInt64(runReader["Id"]); } else { thisId = 0; }
                string thisName;
                if (runReader["RunName"] != DBNull.Value) { thisName = runReader["RunName"].ToString(); } else { thisName = "No Name"; }
                Boolean thisMon;
                if (runReader["RunMon"] != DBNull.Value) { thisMon = Convert.ToBoolean(runReader["RunMon"]); } else { thisMon = false; }
                Boolean thisTues;
                if (runReader["RunTues"] != DBNull.Value) { thisTues = Convert.ToBoolean(runReader["RunTues"]); } else { thisTues = false; }
                Boolean thisWed;
                if (runReader["RunWed"] != DBNull.Value) { thisWed = Convert.ToBoolean(runReader["RunWed"]); } else { thisWed = false; }
                Boolean thisThurs;
                if (runReader["RunThurs"] != DBNull.Value) { thisThurs = Convert.ToBoolean(runReader["RunThurs"]); } else { thisThurs = false; }
                Boolean thisFri;
                if (runReader["RunFri"] != DBNull.Value) { thisFri = Convert.ToBoolean(runReader["RunFri"]); } else { thisFri = false; }
                Boolean thisSat;
                if (runReader["RunSat"] != DBNull.Value) { thisSat = Convert.ToBoolean(runReader["RunSat"]); } else { thisSat = false; }
                Boolean thisSun;
                if (runReader["RunSun"] != DBNull.Value) { thisSun = Convert.ToBoolean(runReader["RunSun"]); } else { thisSun = false; }

                // get Next Run Time from now
                Boolean[] rundays = new Boolean[7] { thisSun, thisMon, thisTues, thisWed, thisThurs, thisFri, thisSat };

                DateTime thisNextRun = DateTime.Now;
                int today = Convert.ToInt32(DateTime.Now.DayOfWeek);
                int i = today;

                do
                {
                    if (rundays[i])
                    {
                        break;
                    }
                    else
                    {
                        thisNextRun = thisNextRun.AddDays(1);
                        i++;
                    }
                    if (i > 6)
                    {
                        i = 0;
                    }
                } while (i != today);

                runs.Add(new Run
                {
                    Id = thisId,                       
                    RunName = thisName,
                    Monday = thisMon,
                    Tuesday = thisTues,
                    Wednesday = thisWed,
                    Thursday = thisThurs,
                    Friday = thisFri,
                    Saturday = thisSat,
                    Sunday = thisSun,
                    NextRun = thisNextRun
                });
            }
            runConn.Close();
        }
        return runs;
    }

    // GET api/values/5
    public Location Get(int id)
    {
        // get tasks from DB 
        Location thisLocation = new Location();

        using (SqlConnection locationConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            SqlCommand locationCommand = new SqlCommand("ct_getlocation", locationConn);
            locationCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter thelocation = locationCommand.Parameters.Add("@location", SqlDbType.Int);
            thelocation.Value = id;

            locationConn.Open();
            SqlDataReader locationReader = locationCommand.ExecuteReader();

            while (locationReader.Read())
            {
                // Add items to task list
                long thisId;
                if (locationReader["Id"] != DBNull.Value) { thisId = Convert.ToInt64(locationReader["Id"]); } else { thisId = 0; }
                string thisName;
                if (locationReader["Name"] != DBNull.Value) { thisName = locationReader["Name"].ToString(); } else { thisName = "No Name"; }
                string thisRun;
                if (locationReader["Run"] != DBNull.Value) { thisRun = locationReader["Run"].ToString(); } else { thisRun = "No Run"; }
                string thisStoreName;
                if (locationReader["StoreName"] != DBNull.Value) { thisStoreName = locationReader["StoreName"].ToString(); } else { thisStoreName = "No Store Name"; }

                thisLocation.Id = thisId;
                thisLocation.Name = thisName;
                thisLocation.Run = thisRun;
                thisLocation.StoreName = thisStoreName;
            }
            locationConn.Close();
        }
        return thisLocation;
    }

    // POST api/values
    public void Post(Run item)
    {
        if (item == null)
        {
            throw new ArgumentNullException("item");
        }
    }

    // PUT api/values/5
    public void Put(int id, Run item)
    {
        if (item == null)
        {
            throw new ArgumentNullException("item");
        }

        using (SqlConnection runConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            // Save the updated crate
            SqlCommand runUpdate = new SqlCommand("ct_updaterun", runConn);
            runUpdate.CommandType = CommandType.StoredProcedure;
            SqlParameter runid = runUpdate.Parameters.Add("@runid", SqlDbType.Int);
            runid.Value = id;
            SqlParameter sunday = runUpdate.Parameters.Add("@sunday", SqlDbType.Bit);
            sunday.Value = item.Monday;
            SqlParameter monday = runUpdate.Parameters.Add("@monday", SqlDbType.Bit);
            monday.Value = item.Monday;
            SqlParameter tuesday = runUpdate.Parameters.Add("@tuesday", SqlDbType.Bit);
            tuesday.Value = item.Monday;
            SqlParameter wednesday = runUpdate.Parameters.Add("@wednesday", SqlDbType.Bit);
            wednesday.Value = item.Monday;
            SqlParameter thursday = runUpdate.Parameters.Add("@thursday", SqlDbType.Bit);
            thursday.Value = item.Monday;
            SqlParameter friday = runUpdate.Parameters.Add("@friday", SqlDbType.Bit);
            friday.Value = item.Monday;
            SqlParameter saturday = runUpdate.Parameters.Add("@saturday", SqlDbType.Bit);
            saturday.Value = item.Monday;

            runConn.Open();
            runUpdate.ExecuteNonQuery();
            runConn.Close();
        }
    }

    // DELETE api/values/5
    public void Delete(int id)
    {
    }


    // PUT, GET, POST, DELETE api/tasks...
    [AllowAnonymous]
    public HttpResponseMessage Options()
    {
        var response = new HttpResponseMessage();
        response.StatusCode = HttpStatusCode.OK;
        return response;
    }
}

0 个答案:

没有答案