![](/img/trans.png)
[英]ASP.NET Web-Api working local but not after deploy on IIS7
[英].NET Web api 2 stops working after sometime in IIS
我在一個解決方案中有兩個Web API項目DLL 。
我的項目解決方案的結構:
在我的解決方案中,項目位於以下位置:
1)基礎解決方案
2)Base Web API
3)模塊Web API
因此,我的解決方案類似於包含許多模塊的BASE解決方案。 每個模塊都可以包含自己的Web API。 此外,我的Base Solution包含自己的Web API
這是我們的結構。
我的問題:
它在我的本地運行解決方案中運行良好。 當我將它托管到IIS時,它正在工作幾個小時,然后通過拋出錯誤消息“找到錯誤404”停止工作。 當我嘗試通過URL直接訪問,如“ http://127.0.0.1:51/Products/Get ”,無法正常工作。
Visual Studio版本:
Visual Studio Professional - 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控制器:
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";
}
}
WebApiConfig.cs對於Module Web API:
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控制器:
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中托管后,我嘗試訪問路徑,如下所示:
對於Base API:
HTTP://本地主機:51600 / API /價值/ GET
返回:
value1
value2
對於模塊API
HTTP://本地主機:51600 / API /模塊/ GET
返回:
value1
value2
我的問題:
過了一段時間,當我嘗試訪問相同的鏈接時,我無法得到它。 它說
status 404. Not Found
我已經在這個問題上工作了3天,但我無法解決問題。
我在stackoverflow中搜索了很多文章,但沒有運氣。
請幫助我擺脫這一點。
謝謝。
如果您擁有Base Web API和Module Web API的所有路由,是否可以檢查Base Solution中的GlobalConfiguration.Configuration.Routes
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.