【问题标题】:ASP.NET Web API not working on AzureASP.NET Web API 无法在 Azure 上运行
【发布时间】:2014-01-07 07:25:31
【问题描述】:

如果 Web API 在本地主机上运行,​​为什么它会停止在 Azure 网站上运行。

错误是加载资源失败:服务器响应状态为 404(未找到)您要查找的资源已被删除,名称已更改,或暂时不可用。在浏览器中将 URL 粘贴到 api 时。

注意: 我从来没有遇到过这个错误,而且我已经有几个使用 Web Api 属性路由的网站。

网络配置

 public static void Register(HttpConfiguration config)
        {

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
}

路由配置

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
             name: "Cities",
             url: "Cities/{id}/{name}/{action}",
             defaults: new { controller = "Cities", action = "Index" }
           );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );         
        }

Global.asax

 AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 

            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

Goggling 显示这是一个常见问题:

HTTP 404 Page Not Found in Web Api hosted in IIS 7.5

WebAPI DELETE request return 404 error in Azure

WebApi and ASP.NET 4 routing issues

https://stackoverflow.com/questions/15805471/using-windows-azure-websites-with-extensionlessurlhandler

Configuring IIS methods for ASP.NET Web API on Windows Azure Websites

How Extensionless URLs Are Handled By ASP.NET v4

Getting 404 from WebAPI on Windows Azure Web Sites

ASP.NET MVC 4 and Web API in Azure - No HTTP resource was found

MVC 4.5 Web API Routing not working?

更新

在尝试了上面链接中建议的所有方法后,我从解决方案中删除了所有 *.dll,创建了一个新的 MVC+Web API 项目并将 *.dll 添加到解决方案中。首先,构建和一切都按预期工作。

【问题讨论】:

  • 您要访问的 URL 是什么?
  • 你也应该启用 CORS。
  • @ThiagoCustodio 这里不需要 CORS。
  • 我有一些问题,但仅限于某些控制器上的某些方法。很奇怪的事

标签: asp.net-mvc azure asp.net-web-api


【解决方案1】:

试试下面两种方法。

方法一尝试如下设置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 runs locally but not on Azure app service

方法二。 确保您在浏览器中的 url 是正确的。在某些情况下您需要添加

azureAPIurl/api/控制器名称

【讨论】:

    【解决方案2】:

    确保您没有任何意外从项目中排除的文件 - 特别是检查 Global.asax 和 Global.asax.cs。排除的文件不会发布,因此可以在本地工作,但不能在 Azure 上工作。

    【讨论】:

      【解决方案3】:

      我通过从 .net Framework 4.6.1 更改为 4.6.2 解决了这个问题。 现在它对我有用

      【讨论】:

        【解决方案4】:

        和@Matija Grcic一样,在Azure上多次成功部署Web API应用程序后,我在新部署中遇到了“您要查找的资源已被删除..” .我从接下来的三个程序集中找到了混合版本控制问题的罪魁祸首:

        System.Net.Http.dll

        System.Net.Http.Formating.dll

        System.Net.Http.WebRequest.dll

        我注意到我的项目对这些程序集的引用被设置为从全局缓存中使用它们,然后在部署中没有复制到 Azure。但我也发现在我的应用程序的错误日志中,它抱怨找不到它们。最后一条线索:它们存在于我的开发机器目录中。

        我从开发目录和 Web.config 中删除了它们,从 runtime>assemblyBinding>dependentAssembly 部分明确提及这些程序集。再次发布,错误占上风。

        然后使用 FTP 手动将引用的三个程序集复制到 Azure 中的 bin 文件夹,瞧!我的 Web API 正在运行!

        我猜 Azure 环境中有新的 Web 应用程序的新版本(写这篇文章的时候是 .Net 4.7),我的旧 Visual Studio 项目可能正在构建 MVC 和 Web API,希望使用旧版本。这样,尝试使用和引用更新的 .Net 框架创建一个新项目一定会有所帮助,就像@Matija Grcic 所做的那样。

        【讨论】:

          【解决方案5】:

          旧帖,你试过了吗:

          <system.webServer>
              <modules runAllManagedModulesForAllRequests="true" />
          </system.webServer>
          

          【讨论】:

          • 旧响应,但对我来说是新问题。非常感谢发帖,刚刚救了我最后一根头发。帮助我将 asp.net Identity 集成到现有的 webApi 中。在我的本地机器上一切正常
          猜你喜欢
          • 2021-10-21
          • 2022-09-27
          • 1970-01-01
          • 2021-05-20
          • 1970-01-01
          • 2021-06-26
          • 2019-07-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多