Reputation: 187
I am using ASP.NET Core
with Angular 2+
, now I have a Controller
like below :
public class ValuesController : Controller
{
[HttpGet]
[Route("api/values/get")]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public IEnumerable<string> Get()
{
return new string[] { "Hello", "DNT" };
}
}
and I have this for startup.CS
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) => {
await next();
if (context.Response.StatusCode == 404 &&
!Path.HasExtension(context.Request.Path.Value) &&
!context.Request.Path.Value.StartsWith("/api/", StringComparison.OrdinalIgnoreCase))
{
context.Request.Path = "/index.html";
await next();
}
});
app.UseMvcWithDefaultRoute();
app.UseDefaultFiles();
app.UseStaticFiles();
}
and this AppComponent
:
export class AppComponent implements OnInit {
constructor(private _httpService: Http) { }
apiValues: string[] = [];
ngOnInit() {
this._httpService.get('api/values/get').subscribe(values => {
this.apiValues = values.json() as string[];
});
}
}
when I run the project I get 404 (bad Request)
.
How can I solve this ?
Upvotes: 1
Views: 1111
Reputation: 424
Your route path is: "api/values/get". So, use this path to your service call:
ngOnInit() {
this._httpService.get('api/values/get').subscribe(values => {
this.apiValues = values.json() as string[];
});
}
Upvotes: 1