1

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 ?

1
  • 1
    if both angular and .net core are in the same project, just adjust your path as /api/values/get and it should work. or else if it doesn't show us how are you configuring angular inside .net core razor view Commented Nov 22, 2017 at 11:05

1 Answer 1

1

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[];
    });
  }
Sign up to request clarification or add additional context in comments.

1 Comment

No it dosn't work . I tried it but same response . also I can not call with Postman too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.