gpt4 book ai didi

.net - 对 .NET 5 API 的 Angular API 请求 - net::ERR_CONNECTION_REFUSED

转载 作者:行者123 更新时间:2023-12-04 18:58:46 25 4
gpt4 key购买 nike

介绍
我正在设置一个 nginx 服务器,其中 angular Universal 作为前端,.NET 5 作为后端 API。
解释
当尝试向 API 发送发布请求时,我得到一个 net::ERR_CONNECTION_REFUSED错误。
您可以在这里自己尝试一下:https://modernamedia.no/#kontakt
我为错误添加了 console.logging。
错误
错误:

POST http://localhost:5000/API/Contact/Contact net::ERR_CONNECTION_REFUSED
error: ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: 'error', …}
headers: ro {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message: "Http failure response for http://localhost:5000/API/Contact/Contact: 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: "http://localhost:5000/API/Contact/Contact"
网络巡检
Error 1
Error 2
这可能是CORS问题吗?
代码
Angular - 联系服务
export class ContactService {
constructor(private http: HttpClient, private toast: ToastService) {}
private SendCTAMessageURL = environment.url + '/API/Contact/Contact';
headers = new HttpHeaders({ 'Content-Type': 'application/json' });
public errorMessage;
public SendContactRequestResult = new BehaviorSubject<boolean>(false);
SendContactRequest(model: any) {
var request = this.http.post<any>(this.SendCTAMessageURL, model);
var response;
request.subscribe({
next: (data) => {
this.toast.Toast(
'Melding sendt!',
'Vi kontakter deg snarest!',
'default',
5000
);
this.SendContactRequestResult.next(true);
response = true;
},
error: (error) => {
this.errorMessage = error.message;
console.error('There was an error!', error);
this.toast.Toast(
'Det oppstod en feil!',
'Kontakt oss på tlf: 902 65 326!',
'error',
10000
);
this.SendContactRequestResult.next(false);
response = false;
},
});
return response;
}
.NET - 启动
       public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerManager logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ModernaMediaDotNet v1"));
}
app.ConfigureExceptionHandler(logger);
app.UseRouting();
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true) // allow any origin
.AllowCredentials()); // allow credentials

//app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}

.NET - 联系 Controller

namespace ModernaMediaDotNet.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ContactController : ControllerBase
{

private ITwillioService twillioService;
private readonly ILoggerManager logger;

public ContactController(ITwillioService twillioService, ILoggerManager logger)
{
this.twillioService = twillioService;
this.logger = logger;
}

[HttpPost]
public IActionResult Contact(ContactModel model)
{
string body = $"Melding fra MODERNA MEDIA: \n" +
$"navn: {model.name} \n" +
$"epost: {model.email} \n" +
$"telefon: {model.phone} \n" +
$"bedrift: {model.business} \n" +
$"tittel: {model.title} \n" +
$"innhold: {model.body}";
logger.LogInfo("Initializing message");
logger.LogInfo(body);
try
{
var result = twillioService.SendMessage(body);
return Ok(result);
}
catch (System.Exception e)
{
logger.LogError($"Something went wrong: {e}");
return StatusCode(500, $"Internal server error: {e}");
}
}
}
}

最佳答案

可以this可以解决您的问题吗?

关于.net - 对 .NET 5 API 的 Angular API 请求 - net::ERR_CONNECTION_REFUSED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71374284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com