簡體   English   中英

ASP.Net Core 6,SPA,端點 - 我無法從 http 調用 controller 獲取請求

[英]ASP.Net Core 6, SPA, endpoint - I can't call a controller from http get request

[已解決] - 評論中的解決方案

[問題]

我是這個行業的新手,所以請原諒我提出一個新手問題,但我現在在 ASP.Net Core 6 上苦苦掙扎,我想我錯過了一些簡單的東西,但我完全被困住了......所以,我嘗試使用 C# 和 Angular SPA 創建一個簡單的應用程序來計算 BMI。 我在.Net Core 5 中完成了它並且工作正常,但是由於某種原因,當我從字面上一對一復制所有功能時,它不想在.Net Core 6 中調用 controller 方法。我開始搜索在 5 和 6 版本之間發生了哪些變化,我看到 Startup.cs 丟失了,相反,他們將其與 Program.cs 合並。 這會是一個“問題”嗎? 您可以在下面找到我的 TS 組件代碼和 controller 代碼。 如果你能給出任何提示可能是什么問題以及為什么它適用於 5,但不適用於 6...

目前,我只想從 BmiController 調用 Get() 方法並接收 200 狀態碼,僅此而已,但每次發送 http 獲取請求時,我都會收到 404 not found。

在此先感謝您的幫助:)

程序.cs

using BMICalculatorCore.Core;
using BMICalculatorCore.Core.Interfaces;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddTransient<ICalculator, MetricCalculator>();

builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html"); ;

bmicalculator.component.ts

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-bmicalculator',
  templateUrl: './bmicalculator.component.html',
  styleUrls: ['./bmicalculator.component.css']
})


export class BmicalculatorComponent implements OnInit {

  public unit: string;

  constructor(private http: HttpClient) {
    this.unit = "Metric";
  }

  ngOnInit(): void {
  }

  sendRequest() {
    this.http.get("https://localhost:44431/" + "bmictrl" + "/calculate").subscribe(result => {
    }, error => console.error(error));
  }
}

BmiController.cs

using Microsoft.AspNetCore.Mvc;

namespace BMICalculatorCore.Controllers
{
    [ApiController]
    [Route("bmictrl")]
    public class BmiController : ControllerBase
    {
        public BmiController()
        {
            
        }

        [HttpGet]
        [Route("calculate")]
        public IActionResult Get()
        {
            return Ok();
        }
    }

一切看起來都很正常。 你能檢查一下你在哪里提出請求嗎? 端口可能已更改。

"this.http.get("https://localhost:44431/" + "bmictrl" + "/calculate")"

我解決了這個問題。 看來,當 VS 創建項目時,它還為所有控制器(路徑)創建了代理。 當我創建新的 controller 時,代理丟失,這就是找不到端點的原因。 我所做的是:

const { env } = require('process');

const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
  env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:21346';

const PROXY_CONFIG = [
  {
    context: [
      "/weather",
      "/bmicalc", // this was added
      "/helloworld", // this was added
      "/bmicalculator" // this was added
   ],
    target: target,
    secure: false
  }
]

module.exports = PROXY_CONFIG;

添加代理允許應用程序命中端點。 五分鍾的工作,半天的搜索……

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM