在.net core项目中想使用类似iis上面虚拟目录的功能

事实上iis是不支持.net core mvc项目虚拟目录的。你在iis上发布网站 然后在wwwroot目录上创建虚拟目录,指向硬盘其他位置上的文件夹,是不会有效果的。

正确的处理方式应该是修改静态文件中间件

这里直接晒出代码:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }
            //注意 这是用来处理请求静态文件的中间件 还有专门针对请求目录的中间件UseDirectoryBrowser()
            //推荐文章 https://www.jb51.net/article/99303.htm
            //使用静态文件 默认指向wwwroot
            app.UseStaticFiles();
            //再插入一个中间件,这次不使用默认的
            app.UseStaticFiles(new StaticFileOptions()
            {
                //FileProvider是专门用来读取静态文件的
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"G:\Video")),
                //如果请求的路径是以下的这个路径 就用上面这个FileProvider
                RequestPath = new PathString("/gVideos")
            });
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"I:\video")),
                RequestPath = new PathString("/iVideos")
            });
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"J:\video")),
                RequestPath = new PathString("/jVideos")
            });
            app.UseCookiePolicy();

            app.UseMvc();
        }

参考的文章:https://www.jb51.net/article/99303.htm 这篇文章非常值得一看