在ASP中获取404错误。NET核心Web API。使用ASP。NET我使用Angular 7作为前端。当我点击表格的保存按钮时,表格数据以角度到达张贴方法,但点击保存后显示404错误。对于这个表单,我创建了第一个模型,在那里我为数据库定义了它的属性,然后我为该模型创建了控制器,并使用我在模型中声明的属性。
角度服务ts文件:
export class MurderService {
BaseUrl :string ='';
constructor( private http:HttpClient, private config:ConfigService) {
this.BaseUrl=config.getApiURI();
}
murderQuestionnaire(data: any){
var murderBody={
Dead: data.Dead,
Wounded: data.Wounded,
CriminalsInvolved: data.CriminalsInvolved,
CriminalAppearance: data.CriminalAppearance,
VehiclesUsed: data.VehiclesUsed,
WeaponsDescription :data.WeaponsDescription
};
return this.http.post(this.BaseUrl +'/Complians' , murderBody);//upto here data reaches
successfully
}
}
此服务的API控制器:
[Route("api/[controller]")]
[ApiController]
public class ComplainMurderController : ControllerBase
{
[HttpPost]
[Route("Complians")]
//api:/Complians
public void PostComplainMurder(Complians complian)
{
var complianMurder = new Complians()
{
Dead = complian.Dead,
Wounded = complian.Wounded,
CriminalsInvolved = complian.CriminalsInvolved,
CriminalAppearence = complian.CriminalAppearence,
VehiclesUsed = complian.VehiclesUsed,
WeaponsDescription = complian.WeaponsDescription
};
try
{
// var result = await complian.Add(complianMurder);
AuthenticationContext authenticationContext = new AuthenticationContext();
authenticationContext.Add(complianMurder);
authenticationContext.SaveChanges();
}
catch(Exception ex)
{
throw ex;
}
}
}
}
错误
POSThttp://localhost:49601/api/Complians404(未找到(
您的控制器路由被定义为api/[controller]
(在您的情况下,这将导致api/ComplainMurder
(。您的操作被定义为Complians
。这些组合是您的实际路线:api/ComplainMurder/Complians
,与api/Complians
不匹配。
用这个改变你的Angular 7侧将修复它:
return this.http.post(this.BaseUrl +'/ComplainMurder/Complians' , murderBody);
旁注:小心拼写错误/不一致(complian而不是抱怨(,因为这势必会在未来导致混乱和/或错误。