使用 ASP.Net 核心制作 api 时出现的问题 [英] A problem while making an api using ASP.Net core

查看:35
本文介绍了使用 ASP.Net 核心制作 api 时出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am trying to make an angular application that takes an api made from asp.net 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.net 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屋!

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