Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .Net核心“Http失败响应404未找到”错误_C#_Angular_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# .Net核心“Http失败响应404未找到”错误

C# .Net核心“Http失败响应404未找到”错误,c#,angular,asp.net-core,asp.net-core-mvc,C#,Angular,Asp.net Core,Asp.net Core Mvc,我刚刚开始进入netcore 2.2,我正试图从angular调用我的控制器,因此,当我从我的服务调用send contact email方法时,我得到一个Http失败响应:404 Not Found 你可以清楚地看到,我在下面的例行程序中明确地写出了路线 sendContactEmail(contact: ISubmitModel): Observable<any> { return this._http.post<any>('api/DataCont

我刚刚开始进入netcore 2.2,我正试图从angular调用我的控制器,因此,当我从我的服务调用send contact email方法时,我得到一个Http失败响应:404 Not Found

你可以清楚地看到,我在下面的例行程序中明确地写出了路线

 sendContactEmail(contact: ISubmitModel): Observable<any> {
        return this._http.post<any>('api/DataController/SendContactEmail/', contact)      
    }
您应该编辑控制器部分,只使用数据

return this._http.post<any>('http://localhost:51889/api/Data/SendContactEmail/', contact)   

正如其他人所提到的,您不应该在路由中使用单词Controller,因为您在路由中使用带有模板[Controller]的route属性,它将只使用该控制器的名称,而不包括Controller单词

为了使其更具可读性,您可以更改api/数据的模板api/[controller],这样就不会导致混淆。 这往往是一个好的做法,因为
如果将来某一天,您出于某种原因决定更改控制器的名称,并且您已将路由绑定到控制器名称,api的消费应用程序将中断,因为路由将发生更改。您不希望这样,而且显然,消费应用程序必须更改对该路由的所有引用才能正常工作。

您到api的路由应为@penleychan提到的“api/Data/SendContactEmail”,数据后面不需要控制器字,当MVC生成路由时,它只使用控制器名,而不是数据DataController@penleychan所以我试过了,现在得到这个错误帖子404找不到还有什么我需要做的吗?我需要注册此控制器吗?@TerranceJackson您确定url与端口正确吗?@Sajeetharan是的,我正在使用dotnet run命令提供给我的url。但是,当我仅从vs2019直接运行IIS时,它可以使用Postman应用程序工作。它不能运行我的angular 8和dot net运行命令。我试过了,它仍然会出错。在进一步测试从vs2019和PostMan运行iis express后,此链接可以工作,但是当我从dotnet运行应用程序并使用localhost链接时,此链接无法工作。因此,在运行dotnet和从vs2019运行iis时,您需要添加http
return this._http.post<any>('http://localhost:51889/api/Data/SendContactEmail/', contact)