Asp.net 删除在Postman中工作但不使用ajax的动词
我已经花了好几个小时研究这个问题,只是想不出来 首先我要让你看看我有什么 js: 我有一个联系人控制器:Asp.net 删除在Postman中工作但不使用ajax的动词,asp.net,asp.net-web-api,Asp.net,Asp.net Web Api,我已经花了好几个小时研究这个问题,只是想不出来 首先我要让你看看我有什么 js: 我有一个联系人控制器: public class ContactController : SimpleController<Contact> { public ContactController (IRepository<Contact> repository) : base(repository) { } } public abstract class Simpl
public class ContactController : SimpleController<Contact>
{
public ContactController (IRepository<Contact> repository) : base(repository)
{
}
}
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();
}
}
我还应该补充一点,它从未到达名为Delete的方法,但它确实到达了简单的控制器构造函数
更新:
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;
}
公共存储库()
{
_context=new AlltechContext();
}
公共虚拟T添加(T实体)
{
if(entity.Id==Guid.Empty)
entity.Id=Guid.NewGuid();
返回_context.Set().Add(实体);
}
公共虚拟无效删除(Guid id)
{
var entity=_context.Set().FirstOrDefault(x=>x.Id==Id);
_context.Set().Remove(实体);
}
公用虚拟机找不到(Guid id)
{
返回_context.Set().FirstOrDefault(x=>x.Id==Id);
}
公共整数保存()
{
尝试
{
返回_context.SaveChanges();
}
捕获(DbEntityValidationException ex)
{
var message=ex.EntityValidationErrors.Select(x=>x.ValidationErrors);
抛出新ArgumentException(message.ToString());
}
}
公共虚拟IEnumerable FindAll()
{
返回_context.Set();
}
公共虚拟T更新(T实体)
{
var actualEntity=_context.Set().FirstOrDefault(x=>x.Id==entity.Id);
重复项(实际情况、实体);
_context.Set().Attach(实际性);
_context.Entry(actualEntity).State=EntityState.Modified;
返回实体;
}
检查这些行是否在您的Webconfig.cs中的App\u 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 }
);
}
}
}
如果该行不可用,请转到package manager控制台
使用此命令
Install-Package Microsoft.AspNet.WebApi.Cors
然后你可能会发现这一行可能会被注释,如果是的话,请取消注释
将此行添加到控制器类
[EnableCors(origins: "http://localhost:55249",headers: "*", methods: "*")]
不要忘记包含名称空间
using System.Web.Http.Cors;
浏览器控制台中有错误吗?我得到了“浏览器链接:无法匹配方法调用MadsKristensen.EditorExtensions.BrowserLink.Menu.MenuBrowserLinkFactory.setVisibility”与存储库classI的更新,我也得到了这个“无法加载XMLHttpRequest。对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。因此,不允许访问源“”。响应的HTTP状态代码为405。“
[EnableCors(origins: "http://localhost:55249",headers: "*", methods: "*")]
using System.Web.Http.Cors;