C# 404访问Web API 2服务时出错
我有一个WebAPI2Web服务,它可以在我的开发站点和本地临时站点上运行,但是当我进入生产环境时,在尝试访问相同的服务时会出现404错误 这里有一个问题的例子C# 404访问Web API 2服务时出错,c#,asp.net,iis,asp.net-web-api2,C#,Asp.net,Iis,Asp.net Web Api2,我有一个WebAPI2Web服务,它可以在我的开发站点和本地临时站点上运行,但是当我进入生产环境时,在尝试访问相同的服务时会出现404错误 这里有一个问题的例子 // production server returns 404 Error http://my.servername.com/AppAPI/CustomerAPI/Session/4 // local stage site works http://localhost:100/AppAPI/CustomerAPI/Session
// production server returns 404 Error
http://my.servername.com/AppAPI/CustomerAPI/Session/4
// local stage site works
http://localhost:100/AppAPI/CustomerAPI/Session/4
我知道有很多原因可以解释为什么会出现404。首先想到的是糟糕的路线,这似乎是我在研究中看到的大多数404个问题的根源。我正在使用WebAPI 2属性路由,如控制器代码中所示
public class SessionController : ApiController
{
[Route("CustomerAPI/Session/{id}")]
[HttpGet]
public string Get(int id)
{
return "value";
}
}
在IIS上,服务所在的站点被设置为另一个站点(my_servername_com)下的“应用程序”(AppAPI)
IIS上的站点结构看起来像
* Server
+ Application Pools
+ Sites
+ my_servername_com
+ AppAPI (application)
+ bin
- default.aspx
- global.asax
- web.config
排除了我所能想到的任何编码或路由问题,这给我留下了某种环境问题。我已经检查了两台服务器上的IIS设置,它们看起来是一样的,所以我不知道还有什么可以看
我的本地环境(工作)
我的生产环境(不工作)
所以我有两种可能,我想
public class Routing
{
public static void RegisterRoutes(HttpConfiguration config)
{
// enable attribute routing
// http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
config.MapHttpAttributeRoutes();
}
}
当然,这在global.asax中被简单地称为
protected void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configure(Routing.RegisterRoutes);
}
任何有人能提供的光线都会有帮助。我快没主意了。下一步是重写ASMX中的服务。。。啊。。。我不想那样做
谢谢,
G来自此链接:检查web.config中的“runAllManagedModulesForAllRequests”是否为true
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
检查IIS中处理程序的顺序:
ExtensionlessUrlHandler-Integrated-4.0
应该放在StaticFile
之前,否则您的api调用将由StaticFile
处理,结果是404
如果您的web.config中不需要处理程序,您可以完全删除它:
<handlers>
<remove name="StaticFile"/>
</handlers>
还有一个类似的线程。可能是您的生产IIS不支持无扩展URL。这可能会有帮助:谢谢你的想法!!我在IIS中的“处理程序映射”中启用了ExtensionlessUrlHandler-ISAPI-4.0_32位、ExtensionlessUrlHandler-ISAPI-4.0_64位、ExtensionlessUrlHandler-Integrated-4.0。请尝试@Yuval Itzhakov-感谢您的想法。。。不变though@Khanh-有没有办法确定链接中引用的补丁是否已经安装?该死!我以前从未听说过这个,但它似乎解决了这个问题!非常感谢。这个建议似乎在整个堆栈溢出中都被给出和接受,但在通过托管模块运行所有请求之前,有很多事情需要注意。我鼓励阅读此答案的人做一些研究。
protected void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configure(Routing.RegisterRoutes);
}
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<handlers>
<remove name="StaticFile"/>
</handlers>