[英]ERR_CONNECTION_REFUSED Asp.Net Core + Angular 10
I'm following a tutorial using Asp.net plus angular server using CORS package.我正在关注使用 Asp.net 和 angular 服务器的教程,使用 CORS ZEFE90A8E604A7C840E88D03A76 Basically I'm trying to execute a post request:
基本上我正在尝试执行发布请求:
// POST: api/Trackers
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
[HttpPost]
public async Task<ActionResult<Tracker>> PostTracker(Tracker tracker)
{
_context.Tracker.Add(tracker);
await _context.SaveChangesAsync();
return CreatedAtAction("GetTracker", new { id = tracker.entryId }, tracker);
}
using this service on Angular Front-End:在 Angular 前端使用此服务:
export class TrackerDetailService {
formData: TrackerDetail
readonly rootURL = "http://localhost:55354/api"
constructor(private http: HttpClient) { }
postTracker(formData:TrackerDetail) {
return this.http.post(this.rootURL + '/Trackers' , formData);
}
}
and I'm getting this error:我收到了这个错误:
I'm quite inexperienced at troubleshooting HTTP errors so any help would be appreciated.我在解决 HTTP 错误方面经验不足,因此不胜感激。
Update: I've been losing my sleep over this for the past 3 days.更新:在过去的 3 天里,我一直为此失眠。 I've tried a different Angular+.Net Tutorial and I'm getting stuck at the same point, not being able to send data from the front-end.
我尝试了不同的 Angular+.Net 教程,但我被困在同一点,无法从前端发送数据。 :( Help!
:( 帮助!
try this:尝试这个:
[HttpPost]
public async Task<ActionResult> PostTracker(Tracker tracker)
{
_context.Tracker.Add(tracker);
await _context.SaveChangesAsync();
return Ok(Tracker)
}
or this或这个
[HttpPost]
public async Task<ActionResult> PostTracker(Tracker tracker)
{
_context.Tracker.Add(tracker);
await _context.SaveChangesAsync();
return CreatedAtAction("GetTracker", new { id = tracker.entryId }, tracker);
}
thanks for replying.感谢回复。 The problem is that I'm using parallels to run windows on my mac and I was trying to access the endpoints based on windows from the view on Mac.
问题是我正在使用并行程序在我的 Mac 上运行 windows 并且我试图从 Mac 上的视图访问基于 windows 的端点。 It was never going to work:)
它永远不会起作用:)
Have you allowed CORS on your Startup.cs?您是否在 Startup.cs 上允许了 CORS? Show your Startup.cs file.
显示您的 Startup.cs 文件。 It should be like this.
应该是这样的。
public void ConfigureServices(IServiceCollection services)
{
// create cors policy
services.AddCors(options =>
options.AddPolicy("DefaultCorsPolicy", builder => builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod())
);
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseCors(); //must be placed after UseRouting , but before UseAuthorization
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
//USAGE:
[EnableCors("DefaultCorsPolicy")]
[Route("api/tracker")]
public class TrackerController : ControllerBase
{
[HttpGet]
public string Get() => "API controller is running";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.