路由不适用于自托管的Web API

编程入门 行业动态 更新时间:2024-10-22 18:31:56
本文介绍了路由不适用于自托管的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:17:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   不适用于   API   Web

发布评论

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

>www.elefans.com

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