2

i am porting over an applciation written in netcore2 to 6 with angular 13. When running in visual studio i always get a 404 error for every web api controller / method

[ApiController]
[Route("[controller]")]
public class GetOptionsController : Controller
{
    private readonly GetOptionsService _getOptionsService;

    public GetOptionsController(GetOptionsService getOptionsService)
    {
        _getOptionsService = getOptionsService; 
    }

    [HttpGet]  
    public IActionResult GetOptions()
    {            
        var options = _getOptionsService.GetOptions();
        return  Ok(options);            
    }

}
}


    import { Injectable } from '@angular/core'; 
    import { HttpClient } from '@angular/common/http';  
    import { Observable } from 'rxjs'; 
    import { IOption } from '../interfaces/option';

 
  
@Injectable()
export class OptionsService {
   
    constructor(private http: HttpClient) { }
  
    getOptions(): Observable<IOption[]> {
      return this.http.get<IOption[]>("getoptions", { responseType: 'json' }); 
    }  
}

when creating the project i chose ASP.NET Core With Angular (maybe this wasnt the best choice?) and just added new api methods and angular components. all web apis return 404 (breakpoints in the controller are never hit) except WeatherForecastController that is created by default when the project is created. i dont see it doing anything different than im doing. this worked in the old project (core 2.0 with angular 6). POST methods dont work either.

i have been trying to figure this out for days and cant. can anyone help?

3
  • Can you make the request using postman/swagger/insomnia etc? Commented Jun 20, 2022 at 0:47
  • i get the same 404 response in postman. when i deploy to iis however, i get a 200 status (i see this in chrome network tab) Commented Jun 20, 2022 at 1:23
  • Hi @Dexter Morgan, I suggest you can run your application on Kestrel and check the output panel to see the port number. .Net 6 split the backend and frontend port number. Commented Jun 20, 2022 at 9:28

2 Answers 2

4

Did you get it fixed?

I met same problem and was stuck for a while, finally I found it's because the Angular proxy need be updated.

Try add your Api in proxy.conf.js files, it works for me:

const PROXY_CONFIG = [
  {
    context: [
      // ...
      "/getoptions"
    ],
    // ...
  }
]

Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm experiencing the same problem, stackoverflow.com/questions/75505538/… Still unable to get the angular app to hit the new endpoint, did you do anything else besides adding additional endpoints to the context ?
0

it is not a good idea to use only controller name as a route. But if you still want it, try to use action routing

[Route("~/GetOptions")]  
public IActionResult GetOptions()

1 Comment

thank you very much for your response this did not seem to work Request URL: localhost:44481/getoptions Request Method: GET Status Code: 404 Not Found Remote Address: 127.0.0.1:44481 Referrer Policy: strict-origin-when-cross-origin i added this to my api controll [HttpGet] [Route("~/GetOptions")] public IActionResult GetOptions()

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.