1
我花了幾個小時研究這個,只是無法弄清楚。DELETE動詞在郵差工作,但不是與AJAX
首先我告訴你我有什麼
JS:
function deleteContact(id) {
let url = baseUrl + "Contact/" + id;
var data = {
id : id
}
$.support.cors = true;
$.ajax({
url: url,
type: 'DELETE',
dataType: 'json',
data:data
});
}
我有聯繫控制器:
public class ContactController : SimpleController<Contact>
{
public ContactController (IRepository<Contact> repository) : base(repository)
{
}
}
和SimpleController:
public abstract class SimpleController<T> : BaseController
where T : class, IEntity, new()
{
private readonly IRepository<T> _repository;
protected SimpleController (IRepository<T> repository)
{
_repository = repository;
}
[HttpGet]
public virtual IEnumerable<T> Get()
{
var list = _repository.FindAll().ToList();
return list;
}
[HttpGet]
public virtual T Get (Guid id)
{
var entity = _repository.Find(id);
if (entity == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
return entity;
}
[HttpPost]
public virtual HttpResponseMessage Post (T entity)
{
if (entity.Id != Guid.Empty)
throw new HttpResponseException(HttpStatusCode.BadRequest);
_repository.Add(entity);
_repository.Save();
var response = Request.CreateResponse<T>(HttpStatusCode.Created, entity);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.Id }));
return response;
}
[HttpPatch]
public virtual T Patch (T entity)
{
var matchingItem = _repository.Find(entity.Id);
if (matchingItem == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
_repository.Update(entity);
_repository.Save();
return entity;
}
[HttpDelete]
public virtual void Delete (Guid id)
{
var matchingItem = _repository.Find(id);
if (matchingItem == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
_repository.Delete(id);
_repository.Save();
}
}
我會說我是postm這工作它刪除項目,但它不使用ajax。
我已經嘗試了很多事情,但仍不斷收到405 與看起來像這樣
{
"name": "Microsoft.ApplicationInsights.Dev.Request",
"time": "2016-11-13T12:41:02.0356067Z",
"tags": {
"ai.device.roleInstance": "James-Desktop.JAILAFILES.com",
"ai.operation.name": "OPTIONS Contact [id]",
"ai.operation.id": "9UbG1l7oEJA=",
"ai.internal.sdkVersion": "web: 2.1.0.363"
},
"data": {
"baseType": "RequestData",
"baseData": {
"ver": 2,
"id": "9UbG1l7oEJA=",
"name": "OPTIONS Contact [id]",
"startTime": "2016-11-13T07:41:02.0356067-05:00",
"duration": "00:00:02.4510180",
"success": false,
"responseCode": "405",
"url": "http://localhost:52136/api/Contact/96d53daa-deca-4dbd-8c6d-2a236387d258",
"httpMethod": "OPTIONS",
"properties": {
"DeveloperMode": "true"
}
}
}
}
響應我還要補充一點,它從來沒有達到所謂的刪除方法,但它確實擊中了簡單的控制器構造。
更新:
public Repository()
{
_context = new AlltechContext();
}
public virtual T Add(T entity)
{
if (entity.Id == Guid.Empty)
entity.Id = Guid.NewGuid();
return _context.Set<T>().Add(entity);
}
public virtual void Delete(Guid id)
{
var entity = _context.Set<T>().FirstOrDefault(x => x.Id == id);
_context.Set<T>().Remove(entity);
}
public virtual T Find(Guid id)
{
return _context.Set<T>().FirstOrDefault(x => x.Id == id);
}
public int Save()
{
try
{
return _context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
var message = ex.EntityValidationErrors.Select(x=>x.ValidationErrors);
throw new ArgumentException(message.ToString());
}
}
public virtual IEnumerable<T> FindAll()
{
return _context.Set<T>();
}
public virtual T Update (T entity)
{
var actualEntity = _context.Set<T>().FirstOrDefault(x => x.Id == entity.Id);
DuplicateItem(actualEntity, entity);
_context.Set<T>().Attach(actualEntity);
_context.Entry(actualEntity).State = EntityState.Modified;
return entity;
}
在您的瀏覽器控制檯的任何錯誤? – Aravind
我得到這個「瀏覽器鏈接:無法匹配方法調用MadsKristensen.EditorExtensions.BrowserLink.Menu.MenuBrowserLinkFactory.setVisibility」 – JamTay317
更新您的存儲庫類 – Aravind