映射到ASP.Net 4中的wwwroot? [英] Map to wwwroot in ASP.Net 4?
问题描述
是否有一种简单的方法可以在ASP.Net v4 Web API中添加一个包含我所有客户端内容的子目录?今天,我已经阅读了很多有关虚拟路径和路由的文章,但是没有什么可以完全描述这种情况.
Is there an easy way to add a subdirectory in ASP.Net v4 Web API that would contain all of my client content? I have read a lot of articles today on virtual paths and routing, but nothing that quite describes this case.
例如,我想将图像存储在wwwroot下,以便当应用程序收到此请求时:
For example, I want to store my images under wwwroot so when the the app receives this request:
它获取wwwroot \ img \ logo.png来处理请求.显然,我不想单独映射每个文件或文件夹.
It fetches wwwroot\img\logo.png to handle the request. Obviously, I don't want to have to map out every file or folder individually.
将有一个Web API静态Web服务,它将由WebApiConfig.cs中的常规路由功能处理.
There will be a Web API restful web service that will be handled by the regular routing functionality in WebApiConfig.cs.
(注意:之所以这样问,是因为我打算在应用程序为GA时将我们的应用程序迁移到ASP.Net v5,这将使移动客户端代码变得微不足道)
(Note: I ask this because I plan to migrate our app to ASP.Net v5 when it is GA, and this would make moving the client-side code trivial)
推荐答案
您可以使用Microsoft.Owin.FileSystem和Microsoft.Owin.StaticFiles NuGet程序包来实现所需的功能.
You can use the Microsoft.Owin.FileSystem and Microsoft.Owin.StaticFiles NuGet Packages to achive what you want.
首先添加两个NuGet程序包.
First add the two NuGet Packages.
然后将此代码添加到您的启动类:
Then add this Code to your Startup class:
public void Configuration(IAppBuilder app)
{
// here your other startup code like app.UseWebApi(config); etc.
ConfigureStaticFiles(app);
}
private void ConfigureStaticFiles(IAppBuilder app)
{
string root = AppDomain.CurrentDomain.BaseDirectory;
string wwwroot = Path.Combine(root, "wwwroot");
var fileServerOptions = new FileServerOptions()
{
EnableDefaultFiles = true,
EnableDirectoryBrowsing = false,
RequestPath = new PathString(string.Empty),
FileSystem = new PhysicalFileSystem(wwwroot)
};
fileServerOptions.StaticFileOptions.ServeUnknownFileTypes = true;
app.UseFileServer(fileServerOptions);
}
还必须确保处理程序已在Web.config文件中注册.它应该看起来像这样:
Also you have to make sure the handler is registered in your Web.config file. It should look like this:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="FormsAuthentication" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="Owin" verb="" path="*" type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"/>
</handlers>
</system.webServer>
然后将自动访问"wwwroot"文件夹中的每个文件.
Then every file in your "wwwroot" Folder will be automatically accessible.
例如,您的wwwroot/img/logo.png文件可以通过 http://yourdomain.com/img/logo进行访问.png ,就像您想要的那样:)
For example your wwwroot/img/logo.png file will be accessible via http://yourdomain.com/img/logo.png, just like you want it :)
如果在构建事件中使用npm/gulp/grunt生成wwwroot文件夹的内容,那么也许还必须编辑csproj文件并添加以下ItemGroup:
If you generate the content of the wwwroot folder with npm/gulp/grunt in a build event, then maybe you also have to edit your csproj file and add this ItemGroup:
<ItemGroup>
<Content Include="wwwroot\**\*" />
</ItemGroup>
这篇关于映射到ASP.Net 4中的wwwroot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!