无法将值从 angular 服务发布到 asp.net core angular 中的 Web API [英] Unable to post values from angular service to Web API in asp.net core angular
问题描述
我正在研究 asp.net core(2.1) angular 项目,在这里我将参数从 angular 服务传递到 Web API,但我没有在 API 中获取这些值.
下面是我的代码,当我为 Web API 使用单独的项目时,它运行良好.现在,当我在 asp.net 核心项目中创建了一个控制器时,我无法在 API 参数列表中获取发布的值.
我是否需要添加/更改一些设置以从 Angular 服务获取值到 API 调用?
角度服务:
getSortedPagedResults(filters): Observable 创业班:
public void ConfigureServices(IServiceCollection services){services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);services.AddSpaStaticFiles(configuration =>{configuration.RootPath = "ClientApp/dist";});services.Configure(options =>{options.MinimumSameSitePolicy = SameSiteMode.None;});services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>{建设者.AllowAnyMethod().AllowAnyHeader()//.WithOrigins("http://localhost:4200").AllowCredentials();}));services.AddSignalR();services.Configure(x =>{x.ValueLengthLimit = int.MaxValue;x.MultipartBodyLengthLimit = int.MaxValue;//如果是多部分});}公共无效配置(IApplicationBuilder 应用程序,IHostingEnvironment 环境){如果 (env.IsDevelopment()){app.UseDeveloperExceptionPage();}别的{app.UseExceptionHandler("/Home/Error");app.UseHsts();}app.UseCors("CorsPolicy");app.UseSignalR(routes =>{route.MapHub("/notify");});app.UseHttpsRedirection();app.UseStaticFiles();app.UseSpaStaticFiles();app.UseMvc(routes =>{路线.MapRoute(名称:默认",模板:{controller}/{action=Index}/{id?}");});app.UseSpa(spa =>{spa.Options.SourcePath = "ClientApp";如果 (env.IsDevelopment()){spa.Options.StartupTimeout = new TimeSpan(0, 0, 100);spa.UseAngularCliServer(npmScript: "start");}});}
解决方案 在你的 API 端使用 FromBody
public IEnumerable员工服务器端([FromBody] 过滤器过滤器)}
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.
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.
Do I need to add/change some settings to get values from angular service to API call?
Angular Service :
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);
}
Startup Class :
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");
}
});
}
解决方案 On your API side use FromBody
public IEnumerable<Users> EmployeesServerSide([FromBody] Filters filters)
}
这篇关于无法将值从 angular 服务发布到 asp.net core angular 中的 Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!