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

问题描述

我很喜欢新的asp.net(asp.net 5 \\核心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.

可以通过路由或其他配置有一个wwwroot文件夹中的一个asp.net 4.5项目以相似的方式,这样的静态文件只提供出来,并且它的web应用程序静态的根文件?

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是从项目文件夹的根加载我一饮而尽文件。这是precisely的那种,我试图避免的事情。

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添加OWIN引用(安装封装Microsoft.Owin.Host.SystemWeb Microsoft.Owin.StaticFiles

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

添加一个类似的启动文件:

Add a startup file similar to this:

[汇编:OwinStartup(typeof运算(MyApp.Startup))]
命名空间MyApp.UI
{
公共类启动
{
    公共无效配置(IAppBuilder应用程序)
    {
        串根= AppDomain.CurrentDomain.BaseDirectory;
        VAR physicalFileSystem =新PhysicalFileSystem(Path.Combine(根的wwwroot));
        VAR的选择=新FileServerOptions
        {
            RequestPath = PathString.Empty,
            EnableDefaultFiles = TRUE,
            文件系统= physicalFileSystem
        };
        options.StaticFileOptions.FileSystem = physicalFileSystem;
        options.StaticFileOptions.ServeUnknownFileTypes = FALSE;
        app.UseFileServer(选件);
    }
}
}

以下内容添加到你的web.config文件,以prevent 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>
    <&处理GT;
      <清除NAME =StaticFile/>
      <添加名称=Owin动词=PATH =*TYPE =Microsoft.Owin.Host.SystemWeb.OwinHttpHandler,Microsoft.Owin.Host.SystemWeb/>
    < /处理器>
  < /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 5样式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 02:59
查看更多