简体   繁体   中英

How to change root path ~/ in Razor in asp.net core

The simplest question for which I can't find an answer.

I have an asp.net core 2.1 MVC application with Razor.

Application widely uses ~/path syntax. Everything works great if application runs from domain root (for example, from http://localhost:5000/ )

But when I run application at non-root (for example, http://localhost:5000/app ) the Razor still uses root ( / ) as base path.

Question: how to configure this? How to specify base path for Razor's ~/ ? There must be an environment variable for it :)

PS: Application run in docker behind the reverse proxy.

在“配置”方法的“启动”类中,使用下一个:

app.UsePathBase("/yourBasePath");

.NET Core 3.1 I Used context.Request.PathBase as following

app.Use((context, next) =>
                {
                    context.Request.Scheme = schemeValue;
#if !DEBUG
                    context.Request.PathBase = $"/{applicationOptions.BasePath}";
#endif
                    return next();
                });

.NET 6 you need to apped app.UseRouting(); also like

var app = builder.Build();

app.UsePathBase("/SomeBasePath");

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
  app.UseSwagger();
  app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.MapControllers();

app.Run();

from milestone: https://github.com/dotnet/aspnetcore/issues/38448

pd: Default route still works so users can hit:

/SomeBasePath/api
/api

workaround idea: https://stackoverflow.com/a/69859449/11813109 not tested

This is .NET 7 milestone so we just need to wait for documentation and fixes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM