我正在研究asp.net core(2.1)角度项目,这里我将参数从角度服务传递到Web API,但是我没有在API中获得这些值。
下面是我的代码,当我为Web API使用单独的项目时,它运行良好。现在,由于我在asp.net核心项目本身中创建了一个控制器,所以我无法在API参数列表中获取发布的值。
我需要添加/更改一些设置来获取从角度服务到API调用的值吗?
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");
}
});
}
答案 0 :(得分:1)
在API方面,使用 const routes: Routes = [
{
path: '', // i.e. remove , (comma) and then insert and save, it'll compile
component: VersionsComponent
}
]
FromBody