使用 ASP.Net 核心制作 api 时出现的问题

编程入门 行业动态 更新时间:2024-10-14 06:25:11
本文介绍了使用 ASP.Net 核心制作 api 时出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试制作一个 Angular 应用程序,该应用程序采用由 asp 核心制作的 api,但是在制作 api 时,它无法正常工作并按计划显示,并且不知道问题出在哪里...我制作了一个 asp 核心网络应用程序.这是model文件夹下的student.cs文件

i am trying to make an angular application that takes an api made from asp core but while making the api , it didn't work and appear as planned and didn't know where was the problem... I made an asp core web app. This is the student.cs file made in the model folder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAPI101.Model
{
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool Pass { get; set; }
    }
}

这也是模型文件夹中的 studentmanager

This is the studentmanager also in model folder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAPI101.Model
{
    public class StudentManager
    {
        public List<Student> GetResults()
        {
            List<Student> oList = new List<Student>();
            var r = new Random();
            for(int i = 0; i < 10; i++)
            {
                var x = new Student();
                x.ID = i;
                x.Name = String.Format("Name{0}", i, ToString());
                x.Pass = (r.Next() % 2 == 0);
                oList.Add(x);
            }
            return oList;
        }
    }
}

这是应该返回aaaaa的代码

This is the code that should return aaaaa

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAPI101.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        [Route("mariam")]
        public string test()
        {
            return "aaaaa";
        }
    }
}

我尝试启动代码,但它没有按计划运行链接:https://localhost:5001/api/Student/mariam

I tried to launch the code and it didn't work as planned o the link:https://localhost:5001/api/Student/mariam

推荐答案

问题是您正在使用 [Route("mariam")] 覆盖控制器路由.

The problem is you are using [Route("mariam")] which overrides the controller route.

您需要改用 [HttpGet(mariam")].

You need to use [HttpGet("mariam")] instead.

    [HttpGet("mariam")]
    public string test()
    {
        return "aaaaa";
    }

使用 HttpGet 将添加到控制器路由.

Using HttpGet will add to the controller route.

这篇关于使用 ASP.Net 核心制作 api 时出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 04:12:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/930434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   ASP   Net   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!