简体   繁体   English

REST 服务错误 405:方法不允许

[英]REST Service Error 405: Method not allowed

My Get Works perfectly, but my update and delete give me 405 Errors.我的 Get 工作正常,但我的更新和删除给了我 405 错误。 Here is my Web.config and Controller.这是我的 Web.config 和控制器。 Sorry for the bad formatting.抱歉格式不正确。 I've tried a lot of different thing but I am unsure why I am getting 405 Errors.我尝试了很多不同的方法,但我不确定为什么会出现 405 错误。 my app works with my other REST Service so I know it can perform CRUD and that the issue is my Service.我的应用程序与我的其他 REST 服务一起使用,所以我知道它可以执行 CRUD,并且问题是我的服务。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        <add key="Username" value="Xamarin" />
        <add key="Password" value="Pa$$w0rd" />
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.5">
            <assemblies>
                <add assembly="System.Net.Http.WebRequest, Version=4.0.0.0, 
    Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
            </assemblies>
        </compilation>
        <httpRuntime targetFramework="4.5" />
        <httpModules>
            <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule,Microsoft.AI.Web" />
        </httpModules>
    </system.web>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
                <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="WebDAVModule"/>
            <remove name="ApplicationInsightsWebTracking" />
            <add name="ApplicationInsightsWebTracking" 
  type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
        </modules>
        <handlers>
            <remove name="WebDAV" />
            <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
            <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <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>
        <directoryBrowse enabled="true" />
        <validation validateIntegratedModeConfiguration="false" />
    </system.webServer>

Here is my Controller这是我的控制器

public class ResponseItemsController : BaseApiController
{
    static readonly IwellService wellService = new wellService(new 
    wellRepository());

    [HttpGet]
    [BasicAuthentication(RequireSsl = false)]
    public HttpResponseMessage Get()
    {
        return base.BuildSuccessResult(HttpStatusCode.OK, 
        wellService.GetData());
    }

    [HttpPost]
    [BasicAuthentication(RequireSsl = false)]
    public HttpResponseMessage Create([FromBody]ResponseItem item)
    {
        try
        {
            if (item == null ||
                string.IsNullOrWhiteSpace(item.Name__c) ||
                string.IsNullOrWhiteSpace(item.Question_1__c))
            {
                return base.BuildErrorResult(HttpStatusCode.BadRequest, ErrorCode.wellItemNameAndNotesRequired.ToString());
            }

            // Determine if the ID already exists
            var itemExists = wellService.DoesItemExist(item.ID);
            if (itemExists)
            {
                return base.BuildErrorResult(HttpStatusCode.Conflict, ErrorCode.wellItemIDInUse.ToString());
            }
            wellService.InsertData(item);
        }
        catch (Exception)
        {
            return base.BuildErrorResult(HttpStatusCode.BadRequest, ErrorCode.CouldNotCreateItem.ToString());
        }

        return base.BuildSuccessResult(HttpStatusCode.Created);
    }

    [HttpPut]
    [BasicAuthentication(RequireSsl = false)]
    public HttpResponseMessage Edit(string id, [FromBody]ResponseItem item)
    {
        try
        {
            if (item == null ||
                string.IsNullOrWhiteSpace(item.Name__c) ||
                string.IsNullOrWhiteSpace(item.Mentor_name__c))
            {
                return base.BuildErrorResult(HttpStatusCode.BadRequest, ErrorCode.wellItemNameAndNotesRequired.ToString());
            }

            var wellItem = wellService.Find(id);
            if (wellItem != null)
            {
                wellService.UpdateData(item);
            }
            else
            {
                return base.BuildErrorResult(HttpStatusCode.NotFound, ErrorCode.RecordNotFound.ToString());
            }
        }
        catch (Exception)
        {
            return base.BuildErrorResult(HttpStatusCode.BadRequest, ErrorCode.CouldNotUpdateItem.ToString());
        }

        return base.BuildSuccessResult(HttpStatusCode.NoContent);
    }

    [HttpDelete]
    [BasicAuthentication(RequireSsl = false)]
    public HttpResponseMessage Delete(string id)
    {
        try
        {
            var wellItem = wellService.Find(id);
            if (wellItem != null)
            {
                wellService.DeleteData(id);
            }
            else
            {
                return base.BuildErrorResult(HttpStatusCode.NotFound, ErrorCode.RecordNotFound.ToString());
            }
        }
        catch (Exception)
        {
            return base.BuildErrorResult(HttpStatusCode.BadRequest, ErrorCode.CouldNotDeleteItem.ToString());
        }

        return base.BuildSuccessResult(HttpStatusCode.NoContent);
    }
}

} }

Here is my routing WebApiconfig/Routeconfig.这是我的路由 WebApiconfig/Routeconfig。 I've tried using [Route("api/responseitems/")] for the controller and then [Route("api/responseitems/{id}")] for the methods but same issue of 405 errors.我试过使用 [Route("api/responseitems/")] 作为控制器,然后使用 [Route("api/responseitems/{id}")] 作为方法,但同样的 405 错误问题。 My written in exceptions are codes are just 404,400,200.我写的异常代码只有 404,400,200。

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
        // Web API routes
        config.MapHttpAttributeRoutes();

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


public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {id = UrlParameter.Optional }
        );
        }
    }

I think the issue is with your calling mechanism from your application.我认为问题在于您的应用程序的调用机制。 Can you use correct Http verb while calling your API.您可以在调用 API 时使用正确的 Http 动词吗?

Or place your complete endpoints with your method type so that I may further help you out.或者将您的完整端点与您的方法类型放在一起,以便我可以进一步帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM