路由不适用于自托管 Web API

编程入门 行业动态 更新时间:2024-10-23 11:17:49
本文介绍了路由不适用于自托管 Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这基本上就是我所拥有的,一组非常简单的三个文件,带有新鲜的 asp core 2.1(实际上是从教程中复制粘贴的):

This is essentially what I have, a very simple set of three files with fresh asp core 2.1 (actually copy-pasted from tutorials):

public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }

然后是最简单的启动

class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(); } }

和默认值控制器

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

不管我怎么称呼我在控制台看到同样的 404 错误:

No matter what I call I see in console same 404 error:

Application started. Press Ctrl+C to shut down. info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET localhost:5000/values info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 105.0181ms 404 info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET localhost:5000/api/values info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 2.6016ms 404 etc

我尝试使用 app.UseMvcWithDefaultRoute(); 添加默认路由并手动指定它.我尝试在使用默认路由时删除路由属性.还尝试添加 AddControllersAsServices().但结果仍然相同 - 404.当我在 app.Run 中设置自定义处理程序时,它可以正常工作.

I tried adding default route both with app.UseMvcWithDefaultRoute(); and specifying it manually. I tried removing route attributes when used the default route. Also tried adding AddControllersAsServices(). But result is still same - 404. When I setup custom handler in app.Run then it works without any issues.

csproj(我已经替换了默认的 Microsoft.AspNetCore.All 依赖项,但路由仍然不起作用)

csproj (I have replaced default Microsoft.AspNetCore.All dependency, but routing still does not work)

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" /> </ItemGroup> </Project>

推荐答案

这很难追查,但问题归结为 .csproj 中的这个:

This was rather difficult to track down, but the problem boils down to this in your .csproj:

<Project Sdk="Microsoft.NET.Sdk">

在构建 Web 应用程序时,您需要改为引用 Web Sdk,如下所示:

As you are building a web application, you need to instead reference the Web Sdk, as follows:

<Project Sdk="Microsoft.NET.Sdk.Web">

我设法通过这个小改动重现并解决了您的问题.

I managed to reproduce and fix your issue with this small change.

更多推荐

路由不适用于自托管 Web API

本文发布于:2023-11-15 04:16:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   不适用于   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!