访问Web API 2服务时出现404错误
我有一个可在我的DEV站点以及本地暂存站点上使用的WebAPI2 Web服务,但是当我进入生产环境时,尝试访问相同的服务时会收到404错误.
I have a WebAPI2 web service that works on my DEV site as well as on a local staging site, but when I push to production I get a 404 error when trying to access the same services.
这是问题的一个例子...
Here's an example of the problem ...
// 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属性路由,如控制器代码中所示...
I know that there are a number of reasons why a 404 could result. Bad routes came to mind first and seem to be the source of most 404 problems that I have seen in my research. I am using WebAPI 2 Attribute routing as seen in the controller code...
public class SessionController : ApiController
{
[Route("CustomerAPI/Session/{id}")]
[HttpGet]
public string Get(int id)
{
return "value";
}
}
在IIS上,该服务所在的站点被设置为另一个站点(my_servername_com)下的应用程序"(AppAPI).
On IIS the site that the service is located in is set up as an "Application" (AppAPI) under another site (my_servername_com).
IIS上的站点结构看起来像...
The site structure on IIS looks like ...
* Server
+ Application Pools
+ Sites
+ my_servername_com
+ AppAPI (application)
+ bin
- default.aspx
- global.asax
- web.config
如您所见,我在网站中包含了一个测试页; default.aspx .此页面在两个位置上均正确显示,因此我知道该站点正在运行并且可以访问,并且至少根URL是正确的.在本地站点上可以访问服务的事实向我表明,路由也已正确设置.
As you can see, I have included a test page in the site; default.aspx. This page displays correctly on BOTH locations, so I know that the site is running and accessible and that at least the root url is correct. The fact that the services are accessible on the local site suggests to me that the Routes are also set up correctly.
http://localhost:100/AppAPI/default.aspx -- works
http://my.servername.com/AppAPI/default.aspx -- works
排除了我能想到的任何编码或路由问题,这使我遇到了某种环境问题.我已经遍历了两台服务器上的IIS设置,它们看起来相同,所以我不确定在那里还要看什么.
Having ruled out any coding or routing issue that I could think of, it leaves me with an environment issue of some sort. I have gone through the IIS settings on both servers and they seem the same, so I am not sure what else to look at there.
我的本地环境(有效)...
My local environment (works) ...
OS: Windows 7 Professional
IIS: IIS 7 (7.5.7600.16385)
.NET Framework : .NET 4.5 (4.5.50938)
我的生产环境(不起作用)...
My production environment (does not work) ...
OS: Windows Web Server 2008
IIS: IIS 7 (7.0.6000.16386)
.NET Framework: .NET 4.5 (4.5.51209)
所以我认为我有两种可能性...
So I am left with two possibilities I think ...
- 网络问题? -我不确定这可能是什么问题.一种 在我的驾驶室外面的小东西.
- 一些被忽略的配置?
- Networking issue? - I am not sure what the problem could be here. A little outside my wheelhouse.
- Some overlooked configuration?
做完一些进一步的研究后,我发现围绕global.asax和WebApiConfig存在许多问题.似乎很相关,因此我在此处添加了它们,但必须注意,这不是不是 MVC应用程序.
After doing some further research I see that there are a number of issues revolving around the global.asax and WebApiConfig. As it seemed that it could be relevant I have added them here but it's important to note that this is not an MVC application.
我没有创建WebApiConfig类,但是创建了一个名为"Routing.cs"的类,该类具有相同的作用.仅仅是为了启用属性路由.
I did not create a WebApiConfig class but created a class called "Routing.cs" that does the same thing. Which is simply to enable the attribute routing.
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中简称为
And of course this is simply called in the global.asax.
protected void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configure(Routing.RegisterRoutes);
}
任何有人掉下的光都会有所帮助.我快要出主意了.下一步是在ASMX中重写服务...哎呀...我不想这样做!
Any light that someone could shed would be helpful. I am about out of ideas. the next move would be to rewrite the services in ASMX ... ugghh ... I don't want to do that!
谢谢, G
通过此链接:
https://weblog .west-wind.com/posts/2011/Mar/27/ASPNET-Routing-not-working-on-IIS-70
From this link:
https://weblog.west-wind.com/posts/2011/Mar/27/ASPNET-Routing-not-working-on-IIS-70
检查web.config中的"runAllManagedModulesForAllRequests"是否为真.
Check that you have "runAllManagedModulesForAllRequests" as true in web.config.
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">