无法将值从angular服务发布到asp.net核心angular中的Web API [英] Unable to post values from angular service to Web API in asp.net core angular
问题描述
我正在研究asp.net core(2.1)角度项目,这里我将参数从角度服务传递到Web API,但是我没有在API中获得这些值.
I am working on asp.net core(2.1) angular project, here I am passing parameters from angular service to web API but I am not getting those values in API.
下面是我的代码,当我为Web API使用单独的项目时,它运行良好.现在,由于我在asp.net核心项目本身中创建了一个控制器,因此无法在API参数列表中获取发布的值.
Below is my code, it was working fine when I was using a separate project for Web API. Now as I have created a controller inside the asp.net core project itself I am unable to get posted values in API parameter list.
我需要添加/更改一些设置来获取从角度服务到API调用的值吗?
Do I need to add/change some settings to get values from angular service to API call?
Angular服务:
getSortedPagedResults(filters): Observable<any> {
debugger;
return this._http.post(this.myAppUrl + 'api/Employee/EmployeesServerSide', filters);
}
API:
[HttpPost]
[Route("api/Employee/EmployeesServerSide")]
public IEnumerable<Users> EmployeesServerSide(Filters filters)
{
return objemployee.GetUsersServerSide(filters);
}
启动类:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
services.Configure<CookiePolicyOptions>(options =>
{
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder
.AllowAnyMethod()
.AllowAnyHeader()
//.WithOrigins("http://localhost:4200")
.AllowCredentials();
}));
services.AddSignalR();
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
{
routes.MapHub<NotifyHub>("/notify");
});
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.Options.StartupTimeout = new TimeSpan(0, 0, 100);
spa.UseAngularCliServer(npmScript: "start");
}
});
}
推荐答案
在您的API端,使用 FromBody
On your API side use FromBody
public IEnumerable<Users> EmployeesServerSide([FromBody] Filters filters)
}
这篇关于无法将值从angular服务发布到asp.net核心angular中的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!