问题
I am trying to add a virtual directory to my dotnet core API. But it gives me a 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
I have set up my api as v1
under the main IIS website as you can see in my IIS project configuration below. The api works fine and I can even reach the /docs
folder in my wwwroot
whichs serves a index.html
(for the custom swagger ui docs).
I can reach the uploads
folder under the the Projects - Api
folder
https://api.project.nl/uploads/profiles/profile.jpeg
but it cannot reach the uploads
folder under the v1
virtual application.
https://api.project.nl/v1/uploads/profiles/profile.jpeg
Any idea why? Am I not sure if its possible at all, would appreciate some pointers on this. I have seen some related questions, for example here but my configuration is just a step futher. They talk about UseFileServer
(docs and they got it to work with that, but I can not seem to get it working it all. Not really sure I need that though.
Not sure if needed but here is my 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");
}
}
回答1:
I am agree with @Tseng but I will add following thing .
ASP.net core is build for multi plateform so it does not handle some request or manage Virtual Directory or Application like IIS in other OS as well. In your case V1 is application in your main application
If you have to do any such thing then you have do following thing.
app.UseFileServer(new FileServerOptions() { FileProvider = new PhysicalFileProvider(<<your physical path>>), RequestPath = new PathString("/V1"), EnableDirectoryBrowsing = true // you make this true or false. });
Once you do this configuration you don't need to configure in IIS.
来源:https://stackoverflow.com/questions/40562149/can-not-access-virtual-directory-under-virtual-dotnet-core-api-application-on-ii