【问题标题】:ASP.Net Core 6, SPA, endpoint - I can't call a controller from http get requestASP.Net Core 6,SPA,端点 - 我无法从 http get 请求调用控制器
【发布时间】:2022-01-16 08:48:38
【问题描述】:

[已解决] - 评论中的解决方案

[问题]

我是这个行业的新手,所以请原谅我提出一个新手问题,但我现在在 ASP.Net Core 6 上苦苦挣扎,我想我错过了一些简单的事情,但我完全被困住了...... 因此,我尝试制作一个简单的应用程序来使用 C# 和 Angular SPA 计算 BMI。我在 .Net Core 5 中做到了,它工作正常,但由于某种原因,当我从字面上一对一地复制所有功能时,它不想在 .Net Core 6 中调用控制器方法。 我开始搜索 5 和 6 版本之间的更改,我发现 Startup.cs 丢失了,相反,他们将其与 Program.cs 合并。这会是一个“问题”吗? 您可以在下面找到我的 TS 组件代码和控制器代码。 如果你能给出任何提示可能是什么问题以及为什么它适用于 5,但不适用于 6...

目前,我只想从BmiController调用Get()方法并接收200状态码,仅此而已,但是每次发送http get请求时,我都会收到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();
        }
    }

【问题讨论】:

  • 您的应用程序是否在 https://localhost:44431 上运行?当您在浏览器中浏览https://localhost:44431/bmictrl/calculate 时会发生什么?
  • 是的,应用程序在端口上运行,检查。当我浏览localhost:44431/bmictrl/calculate 时出现空白页面。邮递员说:“无法获取 /bmictrl/calculate”
  • 如果您解决了您的问题,您可以自行回答以分享您的解决方法!

标签: c# angular asp.net-core single-page-application endpoint


【解决方案1】:

我解决了这个问题。看来,当 VS 创建项目时,它还为所有控制器(路径)创建了代理。当我创建新控制器时,代理丢失了,这就是找不到端点的原因。我所做的是:

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;

添加代理允许应用程序访问端点。五分钟的工作,半天的搜索……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2018-11-26
    相关资源
    最近更新 更多