NetCore 3.1 ApiController在IIS服务器上路由404,但在本地调试时不路由

时间:2020-05-29 21:03:28

标签: asp.net-core-webapi asp.net-core-3.1 iis-10 .net-core-3.1

我有一个非常简单的应用程序,其中包含MVC和API控制器。两者都使用VS 2019和IIS Debugger在本地工作。但是,当我将此项目部署到带有IIS 10的Windows Server 2016时,API控制器会路由所有404。MVC路由会出现问题。

这是我的API控制器代码

[ApiController]
public class LeadController : ControllerBase
{
  [Route( "api/lead/submit" )]
  [HttpPost]
  public async Task<ActionResult<object>> Post( )
  {
     IFormCollection form = await HttpContext.Request.ReadFormAsync();
     IFormFileCollection files = form?.Files;

     var form_fields = form.Select( f => new KeyValuePair<String, String>( f.Key, f.Value ) );
     return ( new { recieved_payload = form_fields } );
  }
}

Program.cs

public class Program
{
   public static void Main( string[] args )
   {
      CreateHostBuilder( args ).Build().Run();
   }

   public static IHostBuilder CreateHostBuilder( string[] args ) =>
       Host.CreateDefaultBuilder( args )
           .ConfigureWebHostDefaults( webBuilder =>
            {
              webBuilder.UseStartup<Startup>();
            } );
 }

Startup.cs

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.AddControllersWithViews().AddNewtonsoftJson();
      }

      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure( IApplicationBuilder app, IWebHostEnvironment env )
      {
         app.UseDeveloperExceptionPage();

         app.UseStaticFiles();

         app.UseRouting();

         app.UseAuthorization();

         app.UseEndpoints( endpoints =>
          {
             endpoints.MapControllerRoute(
                   name: "default",
                   pattern: "{controller=Home}/{action=Index}/{id?}" );
          } );
      }
   }

我尝试将项目发布为自包含且依赖于框架,x86,x64和可移植。没有一个有什么区别。

我还对AppPool设置进行了更改:将.Net 4.0更改为“无托管代码”。这也不会改变应用程序的行为。

这似乎仅对使用[ApiController]的控制器有问题-在普通的MVC控制器中返回json和HttpPost可以正常工作

[HttpPost]
public async Task<ActionResult<object>> Test()
{
   return Json( new { worked = true } );
}

正在运行的本地主机请求: enter image description here

404ing生产要求: enter image description here

编辑:添加了web.config和IIS配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\Etna.Api.Luxury.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

enter image description here

enter image description here

0 个答案:

没有答案