无法访问 IIS 8 上虚拟 dotnet 核心 api 应用程序下的虚拟目录
Can not access virtual directory under virtual dotnet core api application on IIS 8
我正在尝试将虚拟目录添加到我的 dotnet 核心 API。但它给了我一个 404。
This api.project.nl page can’t be found
No web page was found for the web address:
https://api.project.nl/v1/uploads/profiles/profile.jpeg
我已经在主 IIS 网站下将 api 设置为 v1
,您可以在下面的 IIS 项目配置中看到。 api 工作正常,我什至可以到达我的 wwwroot
中的 /docs
文件夹,该文件夹提供 index.html
(用于自定义 swagger ui 文档)。
我可以到达Projects - Api
文件夹下的uploads
文件夹
https://api.project.nl/uploads/profiles/profile.jpeg
但它无法到达v1
虚拟应用程序下的uploads
文件夹。
https://api.project.nl/v1/uploads/profiles/profile.jpeg
知道为什么吗?我不确定它是否可能,希望对此提出一些建议。我看到了一些相关的问题,例如 here but my configuration is just a step futher. They talk about UseFileServer
(docs 并且他们可以解决这个问题,但我似乎无法解决所有问题。虽然不确定我是否需要它。
不确定是否需要,但这是我的 StartUp.cs
public class Startup
{
public IConfigurationRoot Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Project Web Api Docs",
TermsOfService = "None",
Contact = new Contact { Name = "-", Email = "-", Url = "https://project.nl" }
});
});
services.AddMvc(config =>
{
// add policy for a global '[Authorize]' attribute filter,
// so it doesn't have to be placed on all controllers
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
// Configure rollbar error reporting
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddRollbarWeb(Configuration);
services.AddSingleton(Configuration);
// inject an implementation of ISwaggerProvider with defaulted settings applied
services.AddSwaggerGen();
services.AddOptions();
// Inject the api settings
services.Configure<ApiSettings>(Configuration.GetSection("ApiSettings"));
// Configure mappings
Mappings.ConfigureMappings();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
// Configure CORS settings
app.UseCors(builder =>
builder.WithOrigins("http://localhost:9000", "http://localhost:8100", "https://connect.project.nl")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
// Configure rollbar
app.UseRollbarExceptionHandler();
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
Audience = Configuration["ApiSettings:Auth0:ClientId"],
Authority = $"https://{Configuration["ApiSettings:Auth0:Domain"]}/"
});
app.UseMvc();
// enable static files, for the wwwroot (and accompanying docs in it)
// also enable 'default files', such as default.htm, index.html etc.
app.UseDefaultFiles();
app.UseStaticFiles();
// enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger(routeTemplate: "docs/{apiVersion}/swagger.json");
}
}
我同意@Tseng,但我会添加以下内容。
ASP.net 核心是为多平台构建的,因此它不像其他 OS 中的 IIS 那样处理某些请求或管理虚拟目录或应用程序。
在您的情况下,V1 是您主应用程序中的应用程序
如果你必须做任何这样的事情,那么你必须做以下事情。
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(<<your physical path>>),
RequestPath = new PathString("/V1"),
EnableDirectoryBrowsing = true // you make this true or false.
});
完成此配置后,您无需在 IIS 中进行配置。
我正在尝试将虚拟目录添加到我的 dotnet 核心 API。但它给了我一个 404。
This api.project.nl page can’t be found
No web page was found for the web address:
https://api.project.nl/v1/uploads/profiles/profile.jpeg
我已经在主 IIS 网站下将 api 设置为 v1
,您可以在下面的 IIS 项目配置中看到。 api 工作正常,我什至可以到达我的 wwwroot
中的 /docs
文件夹,该文件夹提供 index.html
(用于自定义 swagger ui 文档)。
我可以到达Projects - Api
文件夹下的uploads
文件夹
https://api.project.nl/uploads/profiles/profile.jpeg
但它无法到达v1
虚拟应用程序下的uploads
文件夹。
https://api.project.nl/v1/uploads/profiles/profile.jpeg
知道为什么吗?我不确定它是否可能,希望对此提出一些建议。我看到了一些相关的问题,例如 here but my configuration is just a step futher. They talk about UseFileServer
(docs 并且他们可以解决这个问题,但我似乎无法解决所有问题。虽然不确定我是否需要它。
不确定是否需要,但这是我的 StartUp.cs
public class Startup
{
public IConfigurationRoot Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Project Web Api Docs",
TermsOfService = "None",
Contact = new Contact { Name = "-", Email = "-", Url = "https://project.nl" }
});
});
services.AddMvc(config =>
{
// add policy for a global '[Authorize]' attribute filter,
// so it doesn't have to be placed on all controllers
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
// Configure rollbar error reporting
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddRollbarWeb(Configuration);
services.AddSingleton(Configuration);
// inject an implementation of ISwaggerProvider with defaulted settings applied
services.AddSwaggerGen();
services.AddOptions();
// Inject the api settings
services.Configure<ApiSettings>(Configuration.GetSection("ApiSettings"));
// Configure mappings
Mappings.ConfigureMappings();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
// Configure CORS settings
app.UseCors(builder =>
builder.WithOrigins("http://localhost:9000", "http://localhost:8100", "https://connect.project.nl")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
// Configure rollbar
app.UseRollbarExceptionHandler();
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
Audience = Configuration["ApiSettings:Auth0:ClientId"],
Authority = $"https://{Configuration["ApiSettings:Auth0:Domain"]}/"
});
app.UseMvc();
// enable static files, for the wwwroot (and accompanying docs in it)
// also enable 'default files', such as default.htm, index.html etc.
app.UseDefaultFiles();
app.UseStaticFiles();
// enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger(routeTemplate: "docs/{apiVersion}/swagger.json");
}
}
我同意@Tseng,但我会添加以下内容。
ASP.net 核心是为多平台构建的,因此它不像其他 OS 中的 IIS 那样处理某些请求或管理虚拟目录或应用程序。 在您的情况下,V1 是您主应用程序中的应用程序
如果你必须做任何这样的事情,那么你必须做以下事情。
app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider(<<your physical path>>), RequestPath = new PathString("/V1"), EnableDirectoryBrowsing = true // you make this true or false. });
完成此配置后,您无需在 IIS 中进行配置。