Ich arbeite an einem ASP.NET WebAPI-Projekt mit grundlegenden CRUD-Operationen. Das Projekt wird lokal ausgeführt und enthält eine Beispieldatenbank in Windows Azure.PUT und Löschen funktioniert nicht mit ASP.NET WebAPI und Datenbank unter Windows Azure
Bis jetzt funktioniert das Http GET und POST, gibt mir eine 200 und 201. Aber ich kämpfe mit DELETE und POST. Ich änderte die Handler in der Web.config, entfernte WebDav, aber nichts davon funktionierte. Auch die Aktivierung von CORS und aller Arten von Attributen wie [AcceptVerbs] funktionierte nicht.
Irgendeine Idee, was ich falsch mache?
Fiddler Raw Ausgabe:
HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcTWFyY1xPbmVEcml2ZVxEb2t1bWVudGVcRmlcVnNQcm9qZWt0ZVxONTIwMTQwODI1XE41XE41XGFwaVxwcm9kdWN0XDEwODM=?=
X-Powered-By: ASP.NET
Date: Sun, 14 Sep 2014 15:00:43 GMT
Content-Length: 75
{"Message":"The requested resource does not support http method 'DELETE'."}
Web.config:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Controller:
public class ProductController : BaseApiController
{
public ProductController(IRepository<Product> repo)
: base(repo)
{
}
[HttpGet]
public IEnumerable<Product> Get()
{
//...
}
[HttpGet]
public Product Get(int id)
{
//...
}
[HttpPost]
public HttpResponseMessage Post([FromBody] Product product)
{
//...
}
[HttpPut]
public HttpResponseMessage Put(int productId, [FromBody] Product product)
{
//..
}
[HttpDelete]
public HttpResponseMessage Delete(int productId)
{
//..
}
}
Routing & Formatter:
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));
config.Routes.MapHttpRoute(
name: "Product",
routeTemplate: "api/product/{id}",
defaults: new {controller = "product", id = RouteParameter.Optional }
);
// Custom Formatters:
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(
config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"));
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
Warum rufen Sie das Attribut-Routing auf, aber Sie konfigurieren die Produktroute mithilfe der WebApi2-Vorbereitungsmethode für eine Route? –
Es ist ein Überbleibsel und sollte nicht da sein. Ich aktualisiere es. thx – oldsport