C# 删除事件不作为get方法调用从何处调用
我已经为国家创建了web API,并尝试从angular 7访问它,C# 删除事件不作为get方法调用从何处调用,c#,angular,entity-framework,C#,Angular,Entity Framework,我已经为国家创建了web API,并尝试从angular 7访问它,GET详细信息和GETid记录工作正常,而asDELETE方法不起作用 当我尝试访问DELETE时,没有从angular 7发送到web API的调用 // DELETE: api/Country/5 [HttpDelete("{id}")] public IActionResult Delete(long id) { Country country = _dataRepository.Get(id); if (countr
GET
详细信息和GET
id记录工作正常,而asDELETE
方法不起作用
当我尝试访问DELETE
时,没有从angular 7发送到web API的调用
// DELETE: api/Country/5
[HttpDelete("{id}")]
public IActionResult Delete(long id) {
Country country = _dataRepository.Get(id);
if (country == null) {
return NotFound("The Employee record couldn't be found.");
}
_dataRepository.Delete(country);
return NoContent();
}
// GET: api/Countries
[HttpGet]
public IActionResult Get() {
IEnumerable < Country > country = _dataRepository.GetAll();
return Ok(country);
}
// GET: api/Country/5
[HttpGet("{id}", Name = "Get")]
public IActionResult Get(long id) {
Country country = _dataRepository.Get(id);
if (country == null) {
return NotFound("The Employee record couldn't be found.");
}
return Ok(country);
}
export class CountriesComponent {
public countries: Country[];
bUrl = 'https://localhost:44324/';
constructor(private http: HttpClient, private router: Router) {
this.http.get<Country[]>(this.bUrl + 'api/country').subscribe(result => {
this.countries = result;
}, error => console.error(error));
}
btnClick = function (id) {
this.router.navigateByUrl('/country-edit/' + id);
};
btnDelete = function (id) {
return this.http.delete(this.bUrl + 'api/Country/' + id);
// return this.http.get<Country[]>(this.bUrl + 'api/country/'+ id).subscribe(result => {
// this.countries = result;
// }, error => console.error(error));
}
}
//删除:api/Country/5
[HttpDelete(“{id}”)]
公共IActionResult删除(长id){
Country=\u dataRepository.Get(id);
如果(国家==null){
return NotFound(“找不到员工记录”);
}
_数据存储库。删除(国家);
返回NoContent();
}
//获取:api/国家/地区
[HttpGet]
public IActionResult Get(){
IEnumerableCountry=_dataRepository.GetAll();
返回Ok(国家/地区);
}
//获取:api/Country/5
[HttpGet(“{id}”,Name=“Get”)]
公共IActionResult Get(长id){
Country=\u dataRepository.Get(id);
如果(国家==null){
return NotFound(“找不到员工记录”);
}
返回Ok(国家/地区);
}
导出类国家/地区组件{
公共国家:国家[];
布尔https://localhost:44324/';
构造函数(专用http:HttpClient,专用路由器:路由器){
this.http.get(this.bUrl+'api/country').subscribe(结果=>{
这个国家=结果;
},error=>console.error(error));
}
btnClick=函数(id){
this.router.navigateByUrl('/country edit/'+id);
};
btnDelete=函数(id){
返回this.http.delete(this.bUrl+'api/Country/'+id);
//返回this.http.get(this.bUrl+'api/country/'+id)。订阅(结果=>{
//这个国家=结果;
//},error=>console.error(error));
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddDbContext<DataBaseContext>(opts => opts.UseSqlServer("server=.; database=FoodFactory; Integrated Security=SSPI"));
services.AddScoped<IDataRepository<Country>, CountryManager>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(x => x.AllowAnyMethod());
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
public void配置服务(IServiceCollection服务)
{
services.AddCors();
services.AddDbContext(opts=>opts.UseSqlServer(“server=;database=FoodFactory;integratedsecurity=SSPI”);
services.addScope();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSpaStaticFiles(配置=>
{
configuration.RootPath=“ClientApp/dist”;
});
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseCors(x=>x.AllowAnyMethod());
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller}/{action=Index}/{id?}”);
});
app.UseSpa(spa=>
{
spa.Options.SourcePath=“ClientApp”;
if(env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript:“启动”);
}
});
}
尝试检查您的web API网络配置,可能缺少动词DELETE
尝试添加动词DELETE
,示例如下:
add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
希望这有帮助
更新(针对.NET核心) 步骤1:注册CORS服务 在Startup.ConfigureServices中调用AddCors以将CORS服务添加到应用程序的服务容器:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
}
步骤2:使用CORS中间件启用CORS
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
}
如果这对您有效,请尝试。我的项目中没有web.config文件。有什么解决办法吗?我可以知道你在使用.NET Core吗?是的,它的.NET Core 2.1更新了我的答案,看看是否有帮助。仍然没有启动服务