【发布时间】:2021-02-03 23:15:18
【问题描述】:
我有 Asp.net 核心(在 .net 框架上运行)web mvc 应用程序。
Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
{
EnableQuickPulseMetricStream = true
};
services.AddMvc();
services.AddApplicationInsightsTelemetry(aiOptions);
services.AddCors(option =>
{
option.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("*"));
option.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
TelemetryClient tc = new TelemetryClient();
tc.TrackTrace("Environment: " + env);
if (env.IsDevelopment())
{
tc.TrackTrace("Development");
app.UseDeveloperExceptionPage();
}
else
{
app.UseDeveloperExceptionPage();
}
app.Use((context, next) =>
{
context.Request.PathBase = new PathString("/Application1");
return next.Invoke();
});
app.UseStaticFiles();
app.UseCors(select => select.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
}
HomeController 动作:
public class HomeController : Controller
{
public HomeController(StatelessServiceContext context){
}
public IActionResult UsernameAuthentication()
{
return View();
}
}
动作执行:
http://localhost:9040/Home/UserNameAuthentication
操作未执行:
http://localhost:9040/Application1/Home/UserNameAuthentication
我必须做的任何其他配置,使用 PathBase 执行操作?或任何其他使用上下文路径执行操作的方式。如何?
谢谢。
【问题讨论】:
-
你能用
app.UsePathBase("/Application1");而不是app.Use((context, next) => { context.Request.PathBase = new PathString("/Application1"); return next.Invoke(); });试试吗?我的情况是这样 -
@r08 有什么更新吗?我的回复对你有帮助吗?
标签: c# asp.net asp.net-mvc asp.net-core