New to ASP.NET MVC, I am creating a web application using the Visual Studio 2013 wizard. It creates several folders from where static files are served: Content
, Scripts
, etc.
ASP。NET MVC,我正在使用Visual Studio 2013向导创建一个web应用程序。它创建了几个文件夹,其中包含了静态文件的服务内容:内容、脚本等等。
Frameworks in other languages (e.g. TurboGears) have an explicit directory only for static content, removing the risk of serving the source code of a page instead of processing it which is a typical configuration mistake of PHP sites.
其他语言的框架(例如TurboGears)只对静态内容有一个明确的目录,消除了服务于页面源代码的风险,而不是处理它,这是PHP站点典型的配置错误。
ASP.NET however is happy to deliver anything in the application's root directory, e.g. http://localhost:1740/Project_Readme.html
as long as it has the right extension. Only the Views
folder is protected with a Web.config
.
ASP。但是,NET乐于在应用程序的根目录中提供任何内容,例如http://localhost:1740/project_readme。只要它有正确的扩展名。只有视图文件夹受到Web.config的保护。
How do I configure the application to use another directory than the project's root directory for static files. E.g. if the file favicon.ico
is put into the subdirectory Content
, it should be accessible as http://localhost:1740/favicon.ico
, but nothing outside of the Content
directory unless returned by a controller.
如何将应用程序配置为对静态文件使用项目的根目录之外的另一个目录。例如,如果文件为图标。ico被放入子目录内容中,应该可以作为http://localhost:1740/favicon访问。ico,但是内容目录之外没有内容,除非由控制器返回。
Nothing should ever be executed in this directory, that is, if *.cshtml files are put into this directory, the files' contents (the source code) should be delivered as text/plain
.
这个目录中不应该执行任何内容,也就是说,if *。将cshtml文件放入这个目录中,文件的内容(源代码)应该以文本/plain的形式发送。
Final application will run using mod_mono
on Linux.
最终的应用程序将在Linux上使用mod_mono运行。
2 个解决方案
#1
7
Update:
更新:
Ben,
本,
The proposed solution works only with Owin. To get it working in an MVC application you have to use asp.net MVC 6 (part of asp.net core or asp.net 5) only. But, with Web API you can use the older versions too. To setup the application please use the following steps:
所提出的解决方案仅适用于Owin。要使它在MVC应用程序中工作,您必须只使用asp.net MVC 6 (asp.net核心或asp.net 5的一部分)。但是,有了Web API,您也可以使用旧版本。要设置应用程序,请使用以下步骤:
-
Create an empty project using visual studio templates(don't select Web API or MVC)
使用visual studio模板创建一个空项目(不要选择Web API或MVC)
-
Add the following Nuget packages to the project:
向项目添加以下Nuget包:
Microsoft.AspNet.WebApi
Microsoft.AspNet.WebApi
Microsoft.AspNet.WebApi.Owin
Microsoft.AspNet.WebApi.Owin
Microsoft.Owin.Host.SystemWeb
Microsoft.Owin.Host.SystemWeb
Microsoft.Owin.StaticFiles
Microsoft.Owin.StaticFiles
-
Add a Startup.cs file and decorate the namespace with the following
添加一个启动。cs文件并使用以下内容装饰命名空间
[assembly: OwinStartup(typeof(Startup))]
[组装:OwinStartup(typeof(启动)))
-
Add the following code to the Stratup.cs class
将以下代码添加到Stratup中。计算机科学类
public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {id = RouteParameter.Optional} ); //Configure the file/ static file serving middleware var physicalFileSystem = new PhysicalFileSystem(@".\client"); var fileServerOptions = new FileServerOptions { EnableDefaultFiles = true, RequestPath = PathString.Empty, FileSystem = physicalFileSystem }; fileServerOptions.DefaultFilesOptions.DefaultFileNames = new[] {"index.html"}; fileServerOptions.StaticFileOptions.ServeUnknownFileTypes = true; fileServerOptions.StaticFileOptions.FileSystem = physicalFileSystem; app.UseFileServer(fileServerOptions); app.UseWebApi(config); }
- This should do the magic. Now you can host the application in IIS. IIS will serve the static assets only from client folder. Add Server folder and add controllers.
- 这应该会有神奇的效果。现在可以在IIS中托管应用程序。IIS将只从客户端文件夹中提供静态资产。添加服务器文件夹并添加控制器。
The Microsoft.Owin.Host.SystemWeb is what facilitates the hosting of Owin application in IIS. The file serve options help IIS to serve static files only from client folder.
Microsoft.Owin.Host。SystemWeb有助于在IIS中托管Owin应用程序。文件服务选项帮助IIS只从客户端文件夹提供静态文件。
Please let me know if you have any questions.
如果你有任何问题,请告诉我。
Based on your question, the project structure that you want to achieve should be like the following.
根据您的问题,您希望实现的项目结构应该如下所示。
Basically you will have two folders only, Client and Server. Static files are served from client folder only. Server folder is not accessible. If this is what you need then it can be achieved easily with Owin Self Host with Static File Serving middleware.
基本上你只有两个文件夹,客户端和服务器。仅从客户端文件夹提供静态文件。服务器文件夹不可访问。如果这是您所需要的,那么使用Owin自主机和静态文件服务中间件就可以轻松实现。
Self host works with out any dependency on IIS. But, if your planning to host this application on Linux, you could use Asp.NET CORE 1.0. Later if you decide to host the application on IIS inside windows that can be achieved easily by adding the Microsot.Owin.Host.SystemWeb nuget package.
Self host处理对IIS的任何依赖项。但是,如果您打算在Linux上托管这个应用程序,您可以使用Asp。1.0网络核心。稍后,如果您决定在windows中托管IIS上的应用程序,可以通过添加Microsot.Owin.Host轻松实现。SystemWeb nuget包。
There are great blog posts on this topic. This is the link for one of them. Here is the link for achieving the same in Asp.NET Core.
关于这个话题有很多很棒的博客文章。这是其中一个的链接。这里是在Asp中实现相同的链接。净的核心。
I hope this solves your issues and please let me know if you have any questions.
我希望这能解决你的问题,如果你有任何问题,请告诉我。
Thank you, Soma.
谢谢你,Soma。
#2
3
The best solution I found is to ignore asp.net normal way and write a new way
我发现的最好的解决方案是忽略asp.net常规方法,编写一个新的方法
public override void Init()
{
BeginRequest -= OnBeginRequest;
BeginRequest += OnBeginRequest;
}
protected void OnBeginRequest(object sender, EventArgs e)
{
if (Request.Url.AbsolutePath.StartsWith("/endPoint"))
{
Context.RemapHandler(endPoint);
}
else
{
Context.RemapHandler(staticHandler);
}
}
Let endPoint and staticHandler implement IHttpHandler it works but every static file moves through c# so there might be a solution with better performance
让端点和staticHandler实现IHttpHandler,但每个静态文件都通过c#移动,因此可能有更好的解决方案。
#1
7
Update:
更新:
Ben,
本,
The proposed solution works only with Owin. To get it working in an MVC application you have to use asp.net MVC 6 (part of asp.net core or asp.net 5) only. But, with Web API you can use the older versions too. To setup the application please use the following steps:
所提出的解决方案仅适用于Owin。要使它在MVC应用程序中工作,您必须只使用asp.net MVC 6 (asp.net核心或asp.net 5的一部分)。但是,有了Web API,您也可以使用旧版本。要设置应用程序,请使用以下步骤:
-
Create an empty project using visual studio templates(don't select Web API or MVC)
使用visual studio模板创建一个空项目(不要选择Web API或MVC)
-
Add the following Nuget packages to the project:
向项目添加以下Nuget包:
Microsoft.AspNet.WebApi
Microsoft.AspNet.WebApi
Microsoft.AspNet.WebApi.Owin
Microsoft.AspNet.WebApi.Owin
Microsoft.Owin.Host.SystemWeb
Microsoft.Owin.Host.SystemWeb
Microsoft.Owin.StaticFiles
Microsoft.Owin.StaticFiles
-
Add a Startup.cs file and decorate the namespace with the following
添加一个启动。cs文件并使用以下内容装饰命名空间
[assembly: OwinStartup(typeof(Startup))]
[组装:OwinStartup(typeof(启动)))
-
Add the following code to the Stratup.cs class
将以下代码添加到Stratup中。计算机科学类
public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new {id = RouteParameter.Optional} ); //Configure the file/ static file serving middleware var physicalFileSystem = new PhysicalFileSystem(@".\client"); var fileServerOptions = new FileServerOptions { EnableDefaultFiles = true, RequestPath = PathString.Empty, FileSystem = physicalFileSystem }; fileServerOptions.DefaultFilesOptions.DefaultFileNames = new[] {"index.html"}; fileServerOptions.StaticFileOptions.ServeUnknownFileTypes = true; fileServerOptions.StaticFileOptions.FileSystem = physicalFileSystem; app.UseFileServer(fileServerOptions); app.UseWebApi(config); }
- This should do the magic. Now you can host the application in IIS. IIS will serve the static assets only from client folder. Add Server folder and add controllers.
- 这应该会有神奇的效果。现在可以在IIS中托管应用程序。IIS将只从客户端文件夹中提供静态资产。添加服务器文件夹并添加控制器。
The Microsoft.Owin.Host.SystemWeb is what facilitates the hosting of Owin application in IIS. The file serve options help IIS to serve static files only from client folder.
Microsoft.Owin.Host。SystemWeb有助于在IIS中托管Owin应用程序。文件服务选项帮助IIS只从客户端文件夹提供静态文件。
Please let me know if you have any questions.
如果你有任何问题,请告诉我。
Based on your question, the project structure that you want to achieve should be like the following.
根据您的问题,您希望实现的项目结构应该如下所示。
Basically you will have two folders only, Client and Server. Static files are served from client folder only. Server folder is not accessible. If this is what you need then it can be achieved easily with Owin Self Host with Static File Serving middleware.
基本上你只有两个文件夹,客户端和服务器。仅从客户端文件夹提供静态文件。服务器文件夹不可访问。如果这是您所需要的,那么使用Owin自主机和静态文件服务中间件就可以轻松实现。
Self host works with out any dependency on IIS. But, if your planning to host this application on Linux, you could use Asp.NET CORE 1.0. Later if you decide to host the application on IIS inside windows that can be achieved easily by adding the Microsot.Owin.Host.SystemWeb nuget package.
Self host处理对IIS的任何依赖项。但是,如果您打算在Linux上托管这个应用程序,您可以使用Asp。1.0网络核心。稍后,如果您决定在windows中托管IIS上的应用程序,可以通过添加Microsot.Owin.Host轻松实现。SystemWeb nuget包。
There are great blog posts on this topic. This is the link for one of them. Here is the link for achieving the same in Asp.NET Core.
关于这个话题有很多很棒的博客文章。这是其中一个的链接。这里是在Asp中实现相同的链接。净的核心。
I hope this solves your issues and please let me know if you have any questions.
我希望这能解决你的问题,如果你有任何问题,请告诉我。
Thank you, Soma.
谢谢你,Soma。
#2
3
The best solution I found is to ignore asp.net normal way and write a new way
我发现的最好的解决方案是忽略asp.net常规方法,编写一个新的方法
public override void Init()
{
BeginRequest -= OnBeginRequest;
BeginRequest += OnBeginRequest;
}
protected void OnBeginRequest(object sender, EventArgs e)
{
if (Request.Url.AbsolutePath.StartsWith("/endPoint"))
{
Context.RemapHandler(endPoint);
}
else
{
Context.RemapHandler(staticHandler);
}
}
Let endPoint and staticHandler implement IHttpHandler it works but every static file moves through c# so there might be a solution with better performance
让端点和staticHandler实现IHttpHandler,但每个静态文件都通过c#移动,因此可能有更好的解决方案。