WebApi/.net-core:无法访问webapi

Dav*_*ave 2 .net asp.net-web-api postman .net-core asp.net-core

我无法从邮递员访问 webapi

\n\n

错误是下一个:

\n\n

在此输入图像描述

\n\n

如您所见,没有授权。

\n\n

valuecontroller.cs 是:

\n\n
namespace CVService.Controllers\n{\n    [Route("api/[controller]")]\n\n    [ApiController]\n    public class ValuesController : ControllerBase\n    {\n        // GET api/values\n        [HttpGet]\n        public ActionResult<IEnumerable<string>> Get()\n        {\n            return new string[] { "value1", "value2" };\n        }\n\n        // GET api/values/5\n        [HttpGet("{id}")]\n        public ActionResult<string> Get(int id)\n        {\n            return "value";\n        }\n\n        // POST api/values\n        [HttpPost]\n        public void Post([FromBody] string value)\n        {\n        }\n\n        // PUT api/values/5\n        [HttpPut("{id}")]\n        public void Put(int id, [FromBody] string value)\n        {\n        }\n\n        // DELETE api/values/5\n        [HttpDelete("{id}")]\n        public void Delete(int id)\n        {\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

程序.cs是:

\n\n
namespace CVService\n{\n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n            CreateWebHostBuilder(args).Build().Run();\n        }\n\n        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>\n            WebHost.CreateDefaultBuilder(args)\n                .UseStartup<Startup>();\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

启动.cs是:

\n\n
namespace CVService\n{\n    public class Startup\n    {\n        public Startup(IConfiguration configuration)\n        {\n            Configuration = configuration;\n        }\n\n        public IConfiguration Configuration { get; }\n\n        // This method gets called by the runtime. Use this method to add services to the container.\n        public void ConfigureServices(IServiceCollection services)\n        {\n\n            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>\n            {\n                builder.AllowAnyOrigin()\n                    .AllowAnyMethod()\n                    .AllowAnyHeader();\n            }));\n\n            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);\n        }\n\n        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.\n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\n        {\n            if (env.IsDevelopment())\n            {\n                app.UseDeveloperExceptionPage();\n            }\n            else\n            {\n                app.UseHsts();\n            }\n\n            app.UseHttpsRedirection();\n            app.UseMvc();\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n

点网--信息

\n\n
SDK de .NET Core (reflejando cualquier global.json):\n Version:   2.1.301\n Commit:    59524873d6\n\nEntorno de tiempo de ejecuci\xc3\xb3n:\n OS Name:     Windows\n OS Version:  10.0.16299\n OS Platform: Windows\n RID:         win10-x64\n Base Path:   C:\\Program Files\\dotnet\\sdk\\2.1.301\\\n\nHost (useful for support):\n  Version: 2.1.1\n  Commit:  6985b9f684\n\n.NET Core SDKs installed:\n  2.1.301 [C:\\Program Files\\dotnet\\sdk]\n\n.NET Core runtimes installed:\n  Microsoft.AspNetCore.All 2.1.1 [C:\\Program Files\\dotnet\\shared\\Microsoft.AspNetCore.All]\n  Microsoft.AspNetCore.App 2.1.1 [C:\\Program Files\\dotnet\\shared\\Microsoft.AspNetCore.App]\n  Microsoft.NETCore.App 2.1.1 [C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App]\n\nTo install additional .NET Core runtimes or SDKs:\n  https://aka.ms/dotnet-download\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

Nko*_*osi 5

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

您调用了错误的网址。

GET api/values/get
Run Code Online (Sandbox Code Playgroud)

根据所使用的属性不存在。

称呼

GET api/values 
Run Code Online (Sandbox Code Playgroud)

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

// GET api/values
// GET api/values/5
// POST api/values
// PUT api/values/5
// DELETE api/values/5
Run Code Online (Sandbox Code Playgroud)

参考ASP.NET Core 中控制器操作的路由

  • 好的,Postmant 已激活 SSL 证书验证,我禁用了它。 (4认同)