ASP.NET Core 6,SPA,Endpoint-无法从http GET请求调用控制器 [英] ASP.Net Core 6, SPA, endpoint - I can't call a controller from http get request

查看:39
本文介绍了ASP.NET Core 6,SPA,Endpoint-无法从http GET请求调用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[已解决]-注释中的解决方案

[问题]

我是这个行业的新手,所以请原谅我的一个新手问题,但我现在正在努力使用ASP.NET Core 6,我想我错过了一些简单的东西,但我完全卡住了…… 因此,我尝试使用C#和角度SPA创建一个简单的应用程序来计算BMI。我在.Net Core5中这样做了,它工作得很好,但出于某种原因,当我逐一复制所有函数时,它不想在.Net Core6中调用控制器方法。 我开始搜索5到6个版本之间的更改,我发现Startup.cs不见了,相反,他们将其与Program.cs合并。这会是一个问题吗? 下面你可以找到我的TS组件代码和控制器代码。 如果您能给出任何提示,什么可能是一个问题,以及为什么它可以与5一起工作,但不能与6一起...

目前,我只想从BmiController调用Get()方法并接收200个状态代码,仅此而已,但每次我发送http GET请求时,我都会收到404 Not Found。

事先感谢您的帮助:)

Program.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();
        }
    }

推荐答案

我解决了问题。似乎,当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;

添加代理允许应用程序访问终结点。五分钟的工作,半天的寻找...

这篇关于ASP.NET Core 6,SPA,Endpoint-无法从http GET请求调用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆