我正在使用 Angular 8 和 ASP.Net Core3.1 开发一个应用程序。
当我调用所有 API 时,很少有人工作正常,很少有人给出 400 错误,其中很少有人给出 404 错误。
API 给出 400 错误:
型号详情
public class ServiceOffer
{
public int Id { get; set; }
public string ServiceName { get; set; }
public string ServiceDescription { get; set; }
public int ServicePrice { get; set; }
public bool Status { get; set; }
}
API 详细信息
[Produces("application/json")]
[ApiController]
public class ServiceofferController : ControllerBase
{
[HttpGet]
[Route("api/v1/serviceoffer/allservice")]
public async Task<IActionResult> Index()
{
var objService = new ServiceBL();
var mob = await objService.GetAllServices();
return Ok(mob);
}
[Route("api/v1/serviceoffer/addservices")]
public async Task<IActionResult> AddServices([FromBody] ServiceOffer objSer)
{
var objService = new ServiceBL();
int flag = await objService.AddServiceOffer(objSer);
return Ok(flag);
}
[HttpPut]
[Route("api/v1/serviceoffer/update")]
public static async Task<int> UpdateUser([FromBody] ServiceOffer objSer)
{
var objService = new ServiceBL();
return await objService.UpdateServiceOffer(objSer);
}
}
API 工作正常:api/v1/serviceoffer/allservice
API 给出 400 错误:api/v1/serviceoffer/addservices
API 提供 404 错误:api/v1/serviceoffer/update
角度服务
getAllServices(url: string): Observable<IServiceOffer[]> {
return this.http
.get<IServiceOffer[]>(url)
.pipe(catchError(this.handleError));
}
getServiceById(url: string, id: number): Observable<IServiceOffer> {
const editUrl = `${url}/${id}`;
// console.log(editUrl);
return this.http
.get<IServiceOffer>(editUrl)
.pipe(catchError(this.handleError));
}
// insert new contact details
saveService(url: string, cust: IServiceOffer): Observable<any> {
var Customer = JSON.stringify(cust);
console.log(url);
return this.http
.post(url, Customer, httpOptions)
.pipe(catchError(this.handleError));
}
// update contact details
updateService(url: string, customer: IServiceOffer): Observable<any> {
//const newurl = `${url}/${id}`;
return this.http
.put(url, customer, httpOptions)
.pipe(catchError(this.handleError));
}
配置详情
public class Startup
{
public IConfiguration Configuration { get; }
public static string ConnectionString { get; private set; }
public static Dictionary<string, string> MailSettings { get; private set; }
public Dictionary<string, string> SmsSettings { get; set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
ConnectionString = Configuration.GetSection("ConnectionString").GetSection("SalesContext").Value;
//MailSettings = Configuration.GetSection("SMTP").GetChildren()
// .Select(item => new KeyValuePair<string, string>(item.Key, item.Value))
// .ToDictionary(x => x.Key, x => x.Value);
MailSettings = Configuration.GetSection("SMTP").GetChildren().ToDictionary(x => x.Key, x => x.Value);
services.AddControllersWithViews();
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
//services.AddMvc()
// .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
// .ConfigureApiBehaviorOptions(options =>
// {
// options.SuppressConsumesConstraintForFormFileParameters = true;
// options.SuppressInferBindingSourcesForParameters = true;
// options.SuppressModelStateInvalidFilter = true;
// options.SuppressMapClientErrors = true;
// options.ClientErrorMapping[404].Link = "https://httpstatuses.com/404";
// });
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
}
任何人都可以解释我为什么会遇到这个可怕的错误?
提前致谢。
