Corenet60 Angular总是返回404错误

发布于 2025-02-09 08:42:47 字数 1160 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2025-02-16 08:42:47

你得到了修复吗?

我遇到了同样的问题,并被卡住了一段时间,终于发现这是因为需要更新角度代理。

尝试在proxy.conf.js文件中添加您的API,它对我有用:

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

希望它有帮助。

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.

三人与歌 2025-02-16 08:42:47

仅使用控制器名称作为路线不是一个好主意。但是,如果您仍然需要它,请尝试使用操作路由

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文