gpt4 book ai didi

.net - WebApi/.net-core : Can't access to webapi

转载 作者:行者123 更新时间:2023-12-02 03:33:22 26 4
gpt4 key购买 nike

我无法从 postman 访问 webapi

错误是下一个:

enter image description here

如您所见,没有授权。

valuescontroller.cs 是:

namespace CVService.Controllers
{
[Route("api/[controller]")]

[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

Program.cs 是:

namespace CVService
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}

startup.cs 是:

namespace CVService
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{

services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseMvc();
}
}
}

我以为这是 Cors 的问题,我添加了库,但同样的问题。

我使用的命令是从终端运行 dotnet,我使用的是 SDK 版本 2.1.301。

dotnet --info

SDK de .NET Core (reflejando cualquier global.json):
Version: 2.1.301
Commit: 59524873d6

Entorno de tiempo de ejecución:
OS Name: Windows
OS Version: 10.0.16299
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.1.301\

Host (useful for support):
Version: 2.1.1
Commit: 6985b9f684

.NET Core SDKs installed:
2.1.301 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download

为什么我无法访问?怎么了?

最佳答案

Why can't I access? what is wrong?

您调用了错误的网址。

GET api/values/get
根据使用的属性,

不存在。

调用

GET api/values 

代码示例中的注释实际上显示了根据 Controller 中的操作可以调用哪些路径

// GET api/values
// GET api/values/5
// POST api/values
// PUT api/values/5
// DELETE api/values/5

引用Routing to controller actions in ASP.NET Core

关于.net - WebApi/.net-core : Can't access to webapi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51152191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com