Asp.net mvc ASP.NET Web API在Azure上不起作用

Asp.net mvc ASP.NET Web API在Azure上不起作用,asp.net-mvc,azure,asp.net-web-api,Asp.net Mvc,Azure,Asp.net Web Api,如果Web API在本地主机上工作,为什么它会停止在Azure网站上工作 加载资源失败:服务器响应状态为404(未找到)或您正在查找的资源已被删除、名称已更改或暂时不可用。在浏览器中将URL粘贴到api时 注意: 我从来没有遇到过这个错误,我已经有几个网站已经在azure上使用Web Api属性路由 网络配置 public static void Register(HttpConfiguration config) { config.MapHttpAt

如果Web API在本地主机上工作,为什么它会停止在Azure网站上工作

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

注意: 我从来没有遇到过这个错误,我已经有几个网站已经在azure上使用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 }
            );         
        }
RouteConfig

 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);
目瞪口呆表明这是一个常见问题:

更新


在尝试了上面链接中建议的所有方法后,我已从解决方案中删除了所有*.dll,创建了一个新的MVC+webapi项目,并将*.dll添加到解决方案中。一开始,一切都按预期进行

以前的帖子,但您是否尝试过:

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

与@Matija Grcic相同,在Azure上成功部署了多次Web API应用程序后,我在新部署中遇到了“您正在寻找的资源已被删除…”。我在下面三个程序集中的混合版本控制问题中找到了罪魁祸首:

System.Net.Http.dll

System.Net.Http.Formating.dll

System.Net.Http.WebRequest.dll

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

我从runtime>assemblyBinding>dependentAssembly一节中删除了开发目录和Web.config中对这些程序集的任何明确提及。再次出版,错误占了上风

然后使用FTP和voila手动将引用的三个程序集复制到Azure中的bin文件夹!我的Web API正在工作


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

通过将.net Framework 4.6.1更改为4.6.2,我已经解决了这个问题。
现在它对我有效了

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

尝试以下两种方法

方法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>

方法2. 确保浏览器中的url正确无误。在某些情况下,您需要添加

azureAPIurl/api/controllername


您试图访问的URL是什么?您也应该启用CORS。@ThiagoCustodio这里不需要CORS。我有一些问题,但只是在某些控制器上的某些方法上。非常奇怪的事情对我来说是个新问题。非常感谢你的发帖,刚刚保存了我最后一缕头发。帮助我将asp.net标识集成到现有的webApi中。在我的本地机器上一切正常