问题描述
限时送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:关键词]
发布评论