2016-11-13 8 views
1

私はこれを研究するのに数時間を費やしており、それを理解することはできません。郵便配達員で働いているが、Ajaxで動いていないDELETE動詞

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(); 
    } 
} 

I私は連絡先のコントローラを持って

function deleteContact(id) { 
    let url = baseUrl + "Contact/" + id; 

    var data = { 
     id : id 
    } 

    $.support.cors = true; 
    $.ajax({ 
     url: url, 
     type: 'DELETE', 
     dataType: 'json', 
     data:data 
    }); 
} 

は、最初に私は私が

JSを持っているものをお見せ」私はpostmと言いますこれはアイテムを削除しますが、それはajaxを使用していません。

私は多くのことを試みたが、まだ私はまた、それが削除と呼ばれる方法に到達したことがないことを追加する必要がありますが、それは簡単なコントローラのコンストラクタを打つんこの

{ 
    "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" 
     } 
    } 
    } 
} 

のように見えます応答で405 を得続けるいます。

更新:

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; 
    } 
+0

に任意のエラーを含めることを忘れていけません? – Aravind

+0

私はこの "ブラウザリンク:メソッドの呼び出しMadsKristensen.EditorExtensions.BrowserLink.Menu.MenuBrowserLinkFactory.setVisibility"と – JamTay317

+0

の更新をリポジトリクラス – Aravind

答えて

2

チェックその行は後藤パッケージマネージャコンソール が

このコマンドを使用できない場合は、これらの線はApp_Start

using System.Web.Http; 
namespace WebService 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // New code 
      config.EnableCors();//////////////////////////// 

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

の下であなたのWebconfig.csに存在する場合

Install-Package Microsoft.AspNet.WebApi.Cors 

そして、その行がcommmeかもしれないもしそうなら、コメントしてください。

あなたのコントローラクラスに次の行を追加します

[EnableCors(origins: "http://localhost:55249",headers: "*", methods: "*")] 

は、お使いのブラウザコンソールで名前空間

using System.Web.Http.Cors; 
+0

これはありがとうございました! – JamTay317

関連する問題