0

我在 web-api 中使用了两种方法类型。它在 localhost 中是正确的。但是当我在 godaddy 服务器上使用它时它不正确并且我有错误 405。

[RoutePrefix("api/MyController")]
public class MyController : ApiController
{
    [HttpPut]
    [Route("Method1")]
  
    public returnObject Method1([FromBody]object1 object)
    {
        return  returnObject1
    }
    [HttpPut]
    [Route("Method2")]
    public returnObject2 Method2([FromBody]object2 object)
    {
       return  returnObject2
    }
}

但是我无法访问godaddy服务器中的applicationhost.config,但是我尝试在我的项目中使用此代码添加此部分。

using (ServerManager serverManager = new ServerManager())
            {
                Configuration configAdmin = serverManager.GetApplicationHostConfiguration();
                var section = configAdmin.GetSection("system.webServer/modules", "");
                var collection = section.GetCollection();
                var element = collection.CreateElement();
                element.Attributes["name"].Value = "ExtensionlessUrl-Integrated-4.0";
                element.Attributes["path"].Value = "*.";
                element.Attributes["verb"].Value = "GET,HEAD,POST,DEBUG";
                element.Attributes["type"].Value = "System.Web.Handlers.TransferRequestHandler";
                element.Attributes["preCondition"].Value = "integratedMode,runtimeVersionv4.0";

                collection.Add(element);
                serverManager.CommitChanges();
            } 

                                                                                                                      when i run project and run up to line  element.Attributes["path"] this is null and i have error.
4

2 回答 2

1

我使用 post 方法而不是 put 方法解决了我的问题。谢谢伊普西特·高尔

于 2017-10-05T19:52:25.387 回答
0

只需通过检查 applicationhost.config 文件的行来确保PUT在 GoDaddy 服务器上的 IIS 上启用动词

<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

只需在此处添加PUT动词,因为默认情况下它是禁用的。

于 2017-10-05T11:42:40.097 回答