我不能从邮递员那里访问网页
下一个错误是:

你看不出授权。
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,我使用的是SDKVersion2.1.301。
dotnet --信息
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为什么我不能进入?怎么啦?
发布于 2018-07-03 10:31:02
为什么我不能进入?怎么啦?
您调用了错误的URL。
GET api/values/get根据所使用的属性不存在。
打电话
GET api/values 代码示例中的注释实际上显示了根据控制器中的操作可以调用哪些路径。
// GET api/values
// GET api/values/5
// POST api/values
// PUT api/values/5
// DELETE api/values/5发布于 2022-03-06 18:53:52
解决这一问题的三个步骤:
Startup.cs文件的Configure方法中,添加以下代码:
App.UseEndpoints(端点=> { endpoints.MapControllerRoute(名称:“默认”,模式:"{controller=Home}/{action=Index}/{id?}");});[FromQuery]的模式比您可以像这样调用API更好:
api/Values/Get?id=5https://stackoverflow.com/questions/51152191
复制相似问题