Dav*_*ave 2 .net asp.net-web-api postman .net-core asp.net-core
我无法从邮递员访问 webapi
\n\n错误是下一个:
\n\n\n\n如您所见,没有授权。
\n\nvaluecontroller.cs 是:
\n\nnamespace 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}\nRun Code Online (Sandbox Code Playgroud)\n\n程序.cs是:
\n\nnamespace 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}\nRun Code Online (Sandbox Code Playgroud)\n\n启动.cs是:
\n\nnamespace 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}\nRun Code Online (Sandbox Code Playgroud)\n\n我以为这是 Cors 的问题,我添加了库,但同样的问题。
\n\n我使用的命令是从终端运行 dotnet,我使用的是 SDK 版本 2.1.301。
\n\n点网--信息
\n\nSDK 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\nRun Code Online (Sandbox Code Playgroud)\n\n为什么我无法访问?怎么了?
\n为什么我无法访问?怎么了?
您调用了错误的网址。
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)
| 归档时间: |
|
| 查看次数: |
6951 次 |
| 最近记录: |