加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

对ASP.NET Web API的所有请求返回404错误

发布时间:2020-12-15 19:05:16 所属栏目:asp.Net 来源:网络整理
导读:我有一个ASP.NET MVC 4网站包括Web API。该网站使用Visual Studio 2012和.NET 4.5在Windows 8上使用IIS Express作为Web服务器开发和测试。在这个开发环境中一切正常。 现在它部署在带有IIS 7.5的Windows 2008 R2(SP1)服务器上。安装.NET 4.0和4.5。应用程序
我有一个ASP.NET MVC 4网站包括Web API。该网站使用Visual Studio 2012和.NET 4.5在Windows 8上使用IIS Express作为Web服务器开发和测试。在这个开发环境中一切正常。

现在它部署在带有IIS 7.5的Windows 2008 R2(SP1)服务器上。安装.NET 4.0和4.5。应用程序池以集成管线模式下的.NET 4.0运行。

在这个生产环境中MVC网站工作,Web API没有。对于每个请求,无论GET或POST我得到404错误。如果我只是在浏览器中输入一个Web API网址(在服务器本地打开的IE 9)运行GET请求,我得到一个404页面。如果我从Web API客户端应用程序发出POST请求,我得到一个404以及消息:

No HTTP resource was found that matches the request URI

我已经创建了一个测试网站与MVC 4和Web API以及部署在同一个服务器和Web API工作。 Web API和MVC程序集在两个项目中具有相同的版本号。

此外,我已经添加了Web API Route Debugger的应用程序。如果我使用有效的路由例如http:// myserver / api / order / 12我得到以下结果:

对我来说,这意味着已经找到正确的路由模板Api / {Controller} / {Id},并正确地解析为控制器Order和Id = 12。控制器(派生于ApiController)存在于web程序集中,其中所有的MVC控制器都是。

但是,我不知道状态000是什么意思,为什么没有显示“路由选择”部分(通常情况下,即使程序集不包含单个ApiController,请参见上面链接页面上的屏幕截图) 。不知怎的,它看起来没有找到ApiController,甚至没有搜索或搜索失败。

IIS日志文件不显示任何有用的。更改各种应用程序池设置和使用相同的应用程序池的测试和真正的应用程序没有帮助。

我目前正在从这个过程中删除“功能”,配置设置,第三方程序集等,从应用程序把它降到最小的测试应用程序的小尺寸,希望在某一点它开始工作。

有人有一个线索可能是什么问题?还有任何调试或记录的想法,可能找到的原因是非常欢迎。

编辑

感谢Darrel Miller在下面的评论中的提示我已经集成了Tracing for ASP.NET Web Api。

对于(GET)请求URL http:// myserver / api / order / 12我得到以下:

>在开发环境中,成功(简称):

Message: http://localhost:50020/api/order/12; Category:
System.Web.Http.Request

Controller selection and instantiation…

Operator: DefaultHttpControllerSelector; Operation: SelectController;
Message: Route=”controller:order,id:12″; Category:
System.Web.Http.Controllers

Operator: DefaultHttpControllerSelector; Operation: SelectController;
Message: Order; Category: System.Web.Http.Controllers

Operator: HttpControllerDescriptor; Operation: CreateController;
Message: ; Category: System.Web.Http.Controllers

Operator: DefaultHttpControllerActivator; Operation: Create; Message:
; Category: System.Web.Http.Controllers

Operator: DefaultHttpControllerActivator; Operation: Create; Message:
MyApplication.ApiControllers.OrderController; Category:
System.Web.Http.Controllers

Action selection,parameter binding and action invocation follows…

Content negotiation and formatting for result…

Operator: DefaultContentNegotiator; Operation: Negotiate; Message: Typ = “String” …
more

Disposing the controller…

Operator: OrderController; Operation: Dispose; Message: ; Category:
System.Web.Http.Controllers

>在生产环境中,不成功(简称):

Message: http://myserver/api/order/12; Category:
System.Web.Http.Request

Operator: DefaultHttpControllerSelector; Operation: SelectController;
Message: Route=”controller:order,id:12″; Category:
System.Web.Http.Controllers

The whole part of controller activation,action selection,parameter binding,action invocation is missing and it follows content
negotiation and formatting for the error message immediately
:

Operator: DefaultContentNegotiator; Operation: Negotiate; Message:
Type = “HttpError” … more

解决方法

感谢 Kiran Challa’s评论和来自 this answer的源代码我能够弄清楚,在生产服务器上缺少一个程序集(用于SQL Server Reporting Services的ReportViewer 11程序集)。

虽然在这个程序集中没有ApiController,但是似乎没有找到引用缺少的程序集的控制器(在这种情况下是我的Web项目的程序集)。

显然这个行为与Web API’s DefaultHttpControllerTypeResolver源代码的这段代码有关:

List<Type> result = new List<Type>();

// Go through all assemblies referenced by the application
// and search for types matching a predicate
ICollection<Assembly> assemblies = assembliesResolver.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
    Type[] exportedTypes = null;
    if (assembly == null || assembly.IsDynamic)
    {
        // can't call GetExportedTypes on a dynamic assembly
        continue;
    }

    try
    {
        exportedTypes = assembly.GetExportedTypes();
    }
    catch (ReflectionTypeLoadException ex)
    {
        exportedTypes = ex.Types;
    }
    catch
    {
        // We deliberately ignore all exceptions when building the cache. If 
        // a controller type is not found then we will respond later with a 404.
        // However,until then we don't know whether an exception at all will
        // have an impact on finding a controller.
        continue;
    }

    if (exportedTypes != null)
    {
        result.AddRange(exportedTypes.Where(x => IsControllerTypePredicate(x)));
    }
}

我不知道它是否必须是这样的方式,我不太相信的代码中的意见,但这个catch …继续块是一个可能的问题,而且它花了我大量的时间和挫折沉默找到它。我甚至知道ReportViewer还没有安装。我试图安装它和依赖程序集,但它被服务器上的另一个正在运行的进程阻塞,所以我决定推迟安装,直到我可以联系管理员,专注于MVC和WebAPI测试第一 – 大错误!没有Kiran的调试代码片段,我从来没有想过,一个ReportViewer.dll的存在可能与控制器类型分辨率有关。

在我看来,像我这样的普通开发人员还有改进的余地,他们对Web API的内部工作没有更深入的了解。

安装缺少的ReportViewer.dll后问题消失了。

这里有关于相同症状的问题,可能有相同的原因:

> All ASP.NET Web API controllers return 404
> .Net Web API No HTTP resource was found that matches the request URI
> http://forums.asp.net/t/1861082.aspx/1?All+controllers+break+404+whenever+I+publish+to+Azure

编辑

我已经发出CodePlex的改进要求:

http://aspnetwebstack.codeplex.com/workitem/1075

编辑2(2013年8月11日)

WebAPI v5.0 RC的问题已修复。有关详细信息,请参阅上面的链接到工作项及其注释部分。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读