本文介绍了ASP.NET Core Web API在本地运行,但不在Azure应用程序服务上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在完全关注此链接("> https://docs.microsoft.com/zh-CN/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio)来创建我的网络api.

I have been following this link exactly (https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio) to create my web api.

**在本地,它工作正常.测试链接,并返回JSON数据

**Locally, it is working fine. Tested the links and it returned JSON data

但是,一旦我将Web api部署到azure应用程序服务上,我所有的api链接都已向我返回错误404.我可能错过了路由吗?

However, once I deploy my web api up to azure app service, all my api links have been returning me error 404. Is there anything that I might have missed out for routing?

在我的控制器中,我已将此添加到我的头上.

In my controller I have added this to my head.

[Route("api/xxx")]
[ApiController]

在每个功能中,我都添加了

In each function, I have added this

[HttpPut("xxx/{Id}")]

对于我的程序/启动程序,它与本教程完全相同

As for my program/startup it is totally the same as the tutorial

  1. 程序类

  1. Program class

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

  • Startup.cs

  • Startup.cs

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
    
        app.UseHttpsRedirection();
        app.UseMvc();
    }
    

    让我知道是否需要更多信息.非常感谢您的帮助!

    Let me know if you need anymore information.Really appreciate any help thanks!

    推荐答案

    按如下所示设置 web.config :

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.webServer>
    <handlers>
      <remove name="aspNetCore"/>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
      <aspNetCore processPath="dotnet" arguments=".\Somerandomname.WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
    

    有关更多详细信息,您可以参考以下文章.这是将ASP.NET Core Web API发布到您可以遵循的Azure App Services Web App .

    For more details, you could refer to this article. And here is Publish an ASP.NET Core Web API to an Azure App Services Web App you could follow.

    这篇关于ASP.NET Core Web API在本地运行,但不在Azure应用程序服务上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-27 15:39