我的 Get 工作完美,但我的更新和删除给了我 405 错误。这是我的 Web.config 和控制器。抱歉格式不好。我尝试了很多不同的东西,但我不确定为什么会出现 405 错误。我的应用程序与我的其他 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>
这是我的控制器
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);
}
}
}
这是我的路由 WebApiconfig/Routeconfig。我尝试将 [Route("api/responseitems/")] 用于控制器,然后将 [Route("api/responseitems/{id}")] 用于方法,但同样出现 405 错误。我写的例外是代码只有 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 }
);
}
}