asp.net – 包装StaticFileMiddleware以重定向404错误
发布时间:2020-05-22 21:21:16 所属栏目:asp.Net 来源:互联网
导读:我正在尝试将自定义中间件注入我的OWIN管道,该管道包装了MS提供的StaticFileMiddleware,以支持AngularJS中的 HTML 5模式.我一直在关注这个指南: http://geekswithblogs.net/shaunxu/archive/2014/06/10/host-angularjs-html5mode-in-asp.net-vnext.aspx 从我
|
我正在尝试将自定义中间件注入我的OWIN管道,该管道包装了MS提供的StaticFileMiddleware,以支持AngularJS中的 HTML 5模式.我一直在关注这个指南: http://geekswithblogs.net/shaunxu/archive/2014/06/10/host-angularjs-html5mode-in-asp.net-vnext.aspx 从我可以收集到的如何工作,我的中间件将请求传递给静态文件中间件,然后如果它无法解决这些请求(即,请求HTML 5角度路径,“/无论如何“),它返回基本角度页面,以便HTML 5路径的硬请求将起作用. 我的问题是,调用内部中间件的结果似乎总是200状态代码,即使在我的浏览器中我得到404,这让我挠头.这是我的代码供参考: public static class AngularServerExtension
{
public static IAppBuilder UseAngularServer(this IAppBuilder builder,string rootPath,string entryPath)
{
var options = new AngularServerOptions()
{
FileServerOptions = new FileServerOptions()
{
EnableDirectoryBrowsing = false,FileSystem = new PhysicalFileSystem(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,rootPath))
},EntryPath = new PathString(entryPath)
};
builder.UseDefaultFiles(options.FileServerOptions.DefaultFilesOptions);
return builder.Use(new Func<AppFunc,AppFunc>(next => new AngularServerMiddleware(next,options).Invoke));
}
}
public class AngularServerMiddleware
{
private readonly AngularServerOptions _options;
private readonly AppFunc _next;
private readonly StaticFileMiddleware _innerMiddleware;
public AngularServerMiddleware(AppFunc next,AngularServerOptions options)
{
_next = next;
_options = options;
_innerMiddleware = new StaticFileMiddleware(_next,options.FileServerOptions.StaticFileOptions);
}
public async Task Invoke(IDictionary<string,object> environment)
{
IOwinContext context = new OwinContext(environment);
// try to resolve the request with default static file middleware
await _innerMiddleware.Invoke(environment);
Debug.WriteLine(context.Request.Path + ": " + context.Response.StatusCode);
// *** Right here is where I would expect a 404 but I get a 200 when debugging,// even though my browser eventually returns a 404
// route to root path if the status code is 404
// and need support angular html5mode
if (context.Response.StatusCode == 404 && _options.Html5Mode)
{
context.Request.Path = _options.EntryPath;
await _innerMiddleware.Invoke(environment);
Console.WriteLine(">> " + context.Request.Path + ": " + context.Response.StatusCode);
}
}
}
public class AngularServerOptions
{
public FileServerOptions FileServerOptions { get; set; }
public PathString EntryPath { get; set; }
public bool Html5Mode
{
get
{
return EntryPath.HasValue;
}
}
public AngularServerOptions()
{
FileServerOptions = new FileServerOptions();
EntryPath = PathString.Empty;
}
}
解决方法从您的问题我不确定您是使用IIS还是自我主机.如果您使用的是IIS,那么与使用owin中间件相比,有一个更清洁/更快的解决方案:您可以使用IIS重写引擎,在Web配置中复制以下内容. <system.webServer>
<rewrite>
<rules>
<!--Redirect selected traffic to index -->
<rule name="Index Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
...
</system.webServer>
此行允许正常提供所有文件: <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
这一行允许api正常提供 <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
其他一切都得到index.html (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – MVC3,Ninject和Ninject.MVC3问题
- asp.net-mvc – 避免在Web Publish上删除文件夹
- asp.net-mvc – ASP.net MVC自定义路由处理程序/约束
- asp.net-mvc – MVC 3 – 在视图中显示字典值
- asp.net – 无法加载mysql.web程序集
- 用ASP.NET MVC阻止ZmEu攻击
- asp.net – Knockout.js – 数据绑定的javascript函数
- 如何防止ASP.NET站点的图像热链接?
- asp.net – HTTP错误500.23之后添加到我的本地网站dotless
- asp.net-mvc – 将返回文件的长时间运行进程
推荐文章
站长推荐
- asp.net – 如何强制实体框架插入标识列?
- asp.net – 内容控件必须是顶级控件
- asp.net-mvc – 在ASP.NET MVC中的LinkButton
- 如何以编程方式在ASP.NET MVC中显示/隐藏Razor V
- asp.net-mvc – 具有Windows身份验证的MVC3 Web应
- asp.net-mvc – asp.net mvc博客引擎
- asp.net – 如何防止CPU占用100%,因为iis中的工
- asp.net-mvc – 如何从VOID方法重定向到MVC3中的
- 停止代码构建器转义单引号ASP.NET MVC 2
- asp.net-mvc – ASP.Net 5 project.json脚本命令
热点阅读
