ASP.NET 核心 (HttpSys) 路由在本地工作,但在部署时不工作
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 已启动并且 运行ning 但如果我尝试,它根本无法击中任何控制器。控制器示例之一:
[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
其中 returns 在 404 消息中:未找到,但是如果我在本地 运行:
http://localhost:5000/BasePathOfAPI/api/1234/Home/GetSomeData
它工作正常。
不确定我哪里出错了,也许是 UrlPrefixes
的问题,但如果那不正确,我应该能够访问中间件吗?
也许与路由有关,但为什么它在本地工作?
这可能是 UrlPrefix 中弱通配符的问题。
试试这个用于生产绑定:-
options.UrlPrefixes.Add("https://somehost:30010/BasePathOfAPI/");
其中 somehost
是机器的 FQDN。
已解决 - 必须将完整路径基础添加到 UrlPrefix 和 urlacl 注册中,以便
netsh http add urlacl url=https://*:30010/BasePathOfAPI/ user="NT AUTHORITY\NETWORK SERVICE"
此外,由于控制器位于另一个 dll 中,因此必须在 ConfigureServices 方法中引用程序集:
services.AddMvc().AddApplicationPart(typeof(SystemController).Assembly)
这两个修复使其有效
出于某种原因,当我 运行 在 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 已启动并且 运行ning 但如果我尝试,它根本无法击中任何控制器。控制器示例之一:
[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
其中 returns 在 404 消息中:未找到,但是如果我在本地 运行:
http://localhost:5000/BasePathOfAPI/api/1234/Home/GetSomeData
它工作正常。
不确定我哪里出错了,也许是 UrlPrefixes
的问题,但如果那不正确,我应该能够访问中间件吗?
也许与路由有关,但为什么它在本地工作?
这可能是 UrlPrefix 中弱通配符的问题。
试试这个用于生产绑定:-
options.UrlPrefixes.Add("https://somehost:30010/BasePathOfAPI/");
其中 somehost
是机器的 FQDN。
已解决 - 必须将完整路径基础添加到 UrlPrefix 和 urlacl 注册中,以便
netsh http add urlacl url=https://*:30010/BasePathOfAPI/ user="NT AUTHORITY\NETWORK SERVICE"
此外,由于控制器位于另一个 dll 中,因此必须在 ConfigureServices 方法中引用程序集:
services.AddMvc().AddApplicationPart(typeof(SystemController).Assembly)
这两个修复使其有效