无法在IIS 8上的虚拟dotnet核心api应用程序下访问虚拟目录 [英] Can not access virtual directory under virtual dotnet core api application on IIS 8

查看:157
本文介绍了无法在IIS 8上的虚拟dotnet核心api应用程序下访问虚拟目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将虚拟目录添加到我的dotnet核心API.但这给了我404.

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

我已经在IIS主网站下将api设置为v1,如下面的IIS项目配置所示.该api正常工作,我什至可以到达wwwroot中的/docs文件夹,该文件夹提供index.html(用于自定义swagger ui文档).

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).

可以到达Projects - Api文件夹下的uploads文件夹

I can reach the uploads folder under the the Projects - Api folder

https://api.project.nl/uploads/profiles/profile.jpeg

无法进入v1虚拟应用程序下的uploads文件夹.

but it cannot reach the uploads folder under the v1 virtual application.

https://api.project.nl/v1/uploads/profiles/profile.jpeg

知道为什么吗?我不确定它是否完全可能,对此会提出一些建议.我已经看到一些相关的问题,例如此处,但我的配置仅一步之遥.他们谈论UseFileServer( docs ,可以使用它,但我似乎无法完全使用它.虽然不确定我是否需要它.

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.

不确定是否需要,但这是我的StartUp.cs

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");
    }
}

推荐答案

我同意@Tseng,但我将添加以下内容.

I am agree with @Tseng but I will add following thing .

  1. ASP.net内核是为多平台构建的,因此它不能处理某些请求,也无法管理其他操作系统中的IIS等虚拟目录或应用程序. 在您的情况下,V1是主应用程序中的应用程序

  1. 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.
    });

完成此配置后,无需在IIS中进行配置.

Once you do this configuration you don't need to configure in IIS.

这篇关于无法在IIS 8上的虚拟dotnet核心api应用程序下访问虚拟目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆