我正在尝试将虚拟目录添加到我的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工作正常,我甚至可以访问我的/docs
wwwroot
文件夹,该文件夹提供index.html
(对于自定义swagger ui文档)。
我可以到达uploads
文件夹下的Projects - Api
文件夹
https://api.project.nl/uploads/profiles/profile.jpeg
但无法到达uploads
虚拟应用程序下的v1
文件夹。
https://api.project.nl/v1/uploads/profiles/profile.jpeg
知道为什么吗?我不确定它是否可能,会对此有所了解。我已经看到了一些相关的问题,例如here,但我的配置只是进一步的一步。他们谈论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");
}
}
答案 0 :(得分:2)
我同意@Tseng但我会添加以下内容。
ASP.net核心是针对多平台构建的,因此它不会在其他操作系统中处理某些请求或管理虚拟目录或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中进行配置。