![](/img/trans.png)
[英]Routing error 404 only when deployed in UAT web server. Works locally. ASP.NET Core
[英]ASP.NET Core (HttpSys) Routing works locally but not when deployed
出於某種原因,當我在Windows服務器上使用HttpSys
運行我的ASP.NET核API(通過服務結構)時,路由不起作用,而本地一切正常。 問題是中間件工作正常,所以我知道請求正在處理但它永遠不能打到任何控制器,它只是默認為我的app.run("Some 404 response")
404中間件。 我的一些代碼:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
#region IOC
//ommitted
#endregion
services.AddAutoMapper(typeof(SomeModel));
services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
services.AddDbContext<SomeContext>(options => options.UseSqlServer(_configuration.GetConnectionString("Dev")));
services.AddMvc().AddFluentValidation();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
IdentityModelEventSource.ShowPII = true;
}
//Eliminating that auth is the problem
//app.UseAuthentication();
if (env.IsProduction())
{
app.UseHsts();
app.UseHttpsRedirection();
}
app.UseCors("AllowOrigin");
app.UseMvcWithDefaultRoute(); //tried this instead of below. No luck
//app.UseMvc();
app.Use((context, next) =>
{
if (context.Request.Path.Value == "" || context.Request.Path.Value == "/")
{
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("We're running!");
}
return next.Invoke();
});
app.Run(context =>
{
context.Response.StatusCode = 404;
context.Response.ContentType = "application/json";
return context.Response.WriteAsync("{ \"message\": \"Not found\" }");
});
}
}
Program.cs中:
public static void Main(string[] args)
{
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<SomeContext>();
DbInitializer.Initialize(context);
}
catch (Exception ex)
{
logger.Error(ex, "An error occured while seeding the database");
}
}
host.Run();
}
public static IWebHost CreateWebHostBuilder(string[] args)
{
IHostingEnvironment env = null;
var builder =
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration((hostingContext, config) =>
{
env = hostingContext.HostingEnvironment;
})
.UseHttpSys(options =>
{
if (!env.IsDevelopment())
{
options.UrlPrefixes.Add("https://*:30010/BasePathOfAPI/");
}
else
{
options.UrlPrefixes.Add("http://localhost:5000/BasePathOfAPI/");
}
})
.ConfigureLogging(b =>
{
b.AddApplicationInsights("id here");
})
.UseNLog()
.Build();
return builder;
}
因此除了UrlPrefixes之外,設置幾乎相似。 事實上,我可以通過網關和Windows服務器調用https://somehost/BasePathOfAPI/
並獲取消息We're running!
顯示在瀏覽器中告訴我API已啟動並運行,但如果我嘗試,它根本無法點擊任何控制器。 控制器的一個例子:
[Route("api/{someNumber:int}/Home")]
[ApiController]
public class HomeController: ControllerBase
{
//ctor and props ommitted
[HttpGet("GetSomeData")
[ProducesResponseType(StatusCodes.200OK)]
public async Task<IActionResult> GetSomeData()
{
//implemenetation
}
}
現在,我用來嘗試訪問上述控制器的URL是:
https://somehost/BasePathOfAPI/api/1234/Home/GetSomeData
在404消息中返回:未找到,但是如果我在本地運行:
http://localhost:5000/BasePathOfAPI/api/1234/Home/GetSomeData
它工作正常。
不知道我哪里出錯了,也許是UrlPrefixes
東西,但如果那不正確,我應該能夠找到中間件嗎? 也許與路由有關,但為什么它在本地工作?
已解決 - 必須將完整路徑庫添加到UrlPrefix和urlacl注冊中
netsh http add urlacl url=https://*:30010/BasePathOfAPI/ user="NT AUTHORITY\NETWORK SERVICE"
除此之外,控制器在另一個DLL中,必須在ConfigureServices方法中引用該程序集:
services.AddMvc().AddApplicationPart(typeof(SystemController).Assembly)
這兩個修復使它工作
這可能是UrlPrefix中弱通配符的問題。
請嘗試使用生產綁定: -
options.UrlPrefixes.Add("https://somehost:30010/BasePathOfAPI/");
somehost
是機器的FQDN。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.