Corenet60 Angular总是返回404错误
我正在使用Angular 13在NetCore2中写下的应用程序
[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' });
}
}
。最佳选择?),只是添加了新的API方法和角度组件。除了创建项目时默认创建的WeatherForeCastController外,所有Web API返回404(控制器中的断点永远不会击中)。我看不到它做与我做的不同的事情。这在旧项目(具有Angular 6的Core 2.0)中起作用。发布方法也不起作用。
我一直在努力弄清楚这几天而凝固。谁能帮忙?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你得到了修复吗?
我遇到了同样的问题,并被卡住了一段时间,终于发现这是因为需要更新角度代理。
尝试在proxy.conf.js文件中添加您的API,它对我有用:
希望它有帮助。
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:
Hope it helps.
仅使用控制器名称作为路线不是一个好主意。但是,如果您仍然需要它,请尝试使用操作路由
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