在ASP.NET 4.5项目中使用wwwroot文件夹(ASP.NET Core样式) [英] Using a wwwroot folder (ASP.NET Core style) in ASP.NET 4.5 project

查看:403
本文介绍了在ASP.NET 4.5项目中使用wwwroot文件夹(ASP.NET Core样式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢新的asp.net(asp.net 5 \ core 1.0)Web应用程序的方法,其中以wwwroot文件夹为根,并且仅提供其中的静态文件.

I quite like the approach of the new asp.net (asp.net 5\core 1.0) web apps with the wwwroot folder being the root and only static files in there being served up.

通过路由或其他配置,有可能使asp.net 4.5项目中的wwwroot文件夹具有类似的行为,例如,仅从其中提供静态文件,并且它是webapp的根"(用于静态)文件?

Is possible through routing or other configuration to have a wwwroot folder in an asp.net 4.5 project behave in a similar way, such that static files are only served out of it, and it's the "root" of the webapp for static files?

(问这个问题的部分动机是,我在VS2015的asp.net 5项目中托管了一个角度应用程序,但我需要将其移至asp.net 4.5项目中,但要保留现有的磁盘上的结构)

(Part of my motivation in asking this is that I have an angular app hosted in an asp.net 5 project in VS2015, but I need to move this into an asp.net 4.5 project, but would like to keep the existing structure on disk)

我尝试使用空的ASP.NET 4.5 Web应用程序项目和OWIN进行此操作.因此,我的文件夹结构具有我的有角度的应用程序,在项目文件夹的wwwroot文件夹中有一个主要的index.html文件.项目根目录中没有HTML文件.

I've attempted this using an empty ASP.NET 4.5 web app project and OWIN. So my folder structure has my angular app, with a main index.html file in a wwwroot folder in the project folder. There are no HTML files in the root of the project.

我已经通过nuget和以下启动文件添加了OWIN:

I've added OWIN through nuget and the following startup file:

[assembly: OwinStartup(typeof(MyApp.UI.Startup))]
namespace MyApp.UI
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            string root = AppDomain.CurrentDomain.BaseDirectory;
            var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, "wwwroot"));
            var options = new FileServerOptions
            {
                EnableDefaultFiles = true,
                FileSystem = physicalFileSystem
            };
            options.StaticFileOptions.FileSystem = physicalFileSystem;
            options.StaticFileOptions.ServeUnknownFileTypes = false;
            options.DefaultFilesOptions.DefaultFileNames = new[] {"index.html"};
            app.UseFileServer(options);
        }
    }
}

虽然失败了-我运行此索引时确实加载了index.html文件的源,但是它引用的所有css,js等文件都失败了404.更糟糕的是,如果我将gulpfile.js附加到根url从项目文件夹的根目录加载我的gulp文件.这正是我要避免的事情.

This fails though - the source of my index.html file does load when I run this, but all the css, js, etc files that it references fail with a 404. Even worse, if I append gulpfile.js onto the root url it loads my gulp file from the root of the project folder. This is precisely the sort of thing I'm trying to avoid.

有什么想法吗?

推荐答案

我相信我现在有一个可行的方法来执行此操作.进行了一些谷歌搜索和实验,但是最后我想到了以下过程:

I believe I have a working method for doing this now. Took a bit of googling and experimentation, but in the end I came up with the following process:

  1. 在VS2015中创建一个新的ASP.NET 4.5项目,选择空模板"

  1. Create a new ASP.NET 4.5 project in VS2015, selecting the Empty Template

通过nuget(Install-Package Microsoft.Owin.Host.SystemWebMicrosoft.Owin.StaticFiles)添加OWIN引用

Add OWIN references through nuget (Install-Package Microsoft.Owin.Host.SystemWeb and Microsoft.Owin.StaticFiles)

添加类似于以下内容的启动文件:

Add a startup file similar to this:

[assembly: OwinStartup(typeof(MyApp.Startup))]
namespace MyApp.UI
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            string root = AppDomain.CurrentDomain.BaseDirectory;
            var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, "wwwroot"));
            var options = new FileServerOptions
            {
                RequestPath = PathString.Empty,
                EnableDefaultFiles = true,
                FileSystem = physicalFileSystem
            };
            options.StaticFileOptions.FileSystem = physicalFileSystem;
            options.StaticFileOptions.ServeUnknownFileTypes = false;
            app.UseFileServer(options);
        }
    }
}

  • 将以下内容添加到您的web.config文件中,以防止IIS提供您不希望使用的静态文件,并强制所有内容通过OWIN管道:

  • Add the following to your web.config file, to prevent IIS from serving static files you don't want it to, and force everything through the OWIN pipeline:

    <system.webServer>
        <handlers>
          <remove name="StaticFile"/>
          <add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
        </handlers>
      </system.webServer>
    

  • 我总是乐于接受有关如何执行此操作的更好建议,但这至少似乎对我有用.

    I'm always open to better suggestions on how to do this, but this at least seems to work for me.

    这篇关于在ASP.NET 4.5项目中使用wwwroot文件夹(ASP.NET Core样式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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