Ben*_*lde 6 c# owin owin-middleware
例如,我在服务器上有一个文件/Content/static/home.html.我希望能够/Content/v/1.0.0.0/static/home.html(对于版本控制)发出请求,并让它重写url,以便它使用OWIN中间件访问正确URL的文件.
我目前正在使用URL重写模块(IIS扩展),但我想让它在OWIN管道中工作.
Ben*_*lde 10
我找到了使用Microsoft.Owin.StaticFilesnuget包的解决方案:
首先确保它在您的Web配置中,以便将静态文件请求发送到OWIN:
<system.webServer>
...
<modules runAllManagedModulesForAllRequests="true"></modules>
...
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
然后在您的启动配置方法中,添加以下代码:
// app is your IAppBuilder
app.Use(typeof(MiddlewareUrlRewriter));
app.UseStaticFiles();
app.UseStageMarker(PipelineStage.MapHandler);
Run Code Online (Sandbox Code Playgroud)
这是MiddlewareUrlRewriter:
public class MiddlewareUrlRewriter : OwinMiddleware
{
private static readonly PathString ContentVersioningUrlSegments = PathString.FromUriComponent("/content/v");
public MiddlewareUrlRewriter(OwinMiddleware next)
: base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
PathString remainingPath;
if (context.Request.Path.StartsWithSegments(ContentVersioningUrlSegments, out remainingPath) && remainingPath.HasValue && remainingPath.Value.Length > 1)
{
context.Request.Path = new PathString("/Content" + remainingPath.Value.Substring(remainingPath.Value.IndexOf('/', 1)));
}
await Next.Invoke(context);
}
}
Run Code Online (Sandbox Code Playgroud)
例如,这将允许GET请求/Content/v/1.0.0.0/static/home.html检索文件/Content/static/home.html.
更新:app.UseStageMarker(PipelineStage.MapHandler);在其他app.Use方法之后
添加,因为它需要使其工作.http://katanaproject.codeplex.com/wikipage?title=Static%20Files%20on%20IIS
| 归档时间: |
|
| 查看次数: |
7041 次 |
| 最近记录: |