我在一个解决方案中有两个 Web API 项目 DLL。
我的项目解决方案的结构:
在我的解决方案中,项目位于如下位置:
1) 基础解决方案
2) 基础网络 API
3) 模块网络接口(interface)
因此,我的解决方案类似于包含许多模块的基础解决方案。每个模块都可以包含自己的 Web API。此外,我的基本解决方案包含自己的 Web API
这是我们的结构。
我的问题:
它在我的本地运行解决方案中运行良好。当我将它托管到 IIS 时,它工作了几个小时,然后通过抛出错误消息“找到错误 404”而停止工作。当我尝试通过类似于“http://127.0.0.1:51/Products/Get”的 URL 直接访问时,无法正常工作。
Visual Studio 版本:
Visual Studio 专业版 - 2015
IIS 版本:
IIS - 7.5.7600
我的方法: 我有一个模拟这种情况的简单项目。它与我原来的项目有同样的问题。
基本模块的 Web API:
App_Start 下的 WepApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
基础 API Controller :
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
用于模块 Web API 的 WebApiConfig.cs:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultModuleApi",
routeTemplate: "api/module/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
模块 API Controller :
public class ModulesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
上面代码的注释:
两个APIConfig文件的区别是:
对于模块代码:
routeTemplate: "api/module/{controller}/{action}/{id}"
基本代码:
routeTemplate: "api/{controller}/{action}/{id}"
Global.asax:
namespace BaseSln
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//GlobalConfiguration.Configure(WebApiConfig.Register);
var typesWithMyAttribute =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetLoadableTypes().Where(t1 => t1.Name == "WebApiConfig"
&& t1.GetMethod("Register") != null
&& t1.GetMethod("Register").IsStatic)
select new { Type = t, MethodInfo = t.GetMethod("Register") };
//register all the Routes
foreach (var type in typesWithMyAttribute)
{
var mi = type.MethodInfo;
Action<HttpConfiguration> action = null;
try
{
action = Delegate.CreateDelegate(typeof(Action<HttpConfiguration>), mi) as Action<HttpConfiguration>;
}
catch
{
//ignore the errors for now... should be handled somewhere else or logged.
}
if (action != null) GlobalConfiguration.Configure(action);
}
}
}
我对上述项目的尝试:
在 IIS 中托管后,我尝试访问如下路径:
对于基础 API:
http://localhost:51600/api/Values/Get
返回:
value1
value2
对于模块 API
http://localhost:51600/api/Modules/Get
返回:
value1
value2
我的问题:
一段时间后,当我尝试访问同一个链接时,我无法获得该链接。它说
status 404. Not Found
我已经在这个问题上工作了 3 天,但我无法解决问题。
我在 stackoverflow 中搜索了很多文章,但没有运气。
请帮我摆脱这个。
谢谢。
最佳答案
如果您拥有 Base Web API 和 Module Web API 的所有路由,您能否检查 Base Solution 中的 GlobalConfiguration.Configuration.Routes
?
关于c# - .NET Web api 2 在 IIS 中的某个时间后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468287/