asp.net – 包装StaticFileMiddleware以重定向404错误

前端之家收集整理的这篇文章主要介绍了asp.net – 包装StaticFileMiddleware以重定向404错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将自定义中间件注入我的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,这让我挠头.这是我的代码供参考:

  1. public static class AngularServerExtension
  2. {
  3. public static IAppBuilder UseAngularServer(this IAppBuilder builder,string rootPath,string entryPath)
  4. {
  5. var options = new AngularServerOptions()
  6. {
  7. FileServerOptions = new FileServerOptions()
  8. {
  9. EnableDirectoryBrowsing = false,FileSystem = new PhysicalFileSystem(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,rootPath))
  10. },EntryPath = new PathString(entryPath)
  11. };
  12.  
  13. builder.UseDefaultFiles(options.FileServerOptions.DefaultFilesOptions);
  14. return builder.Use(new Func<AppFunc,AppFunc>(next => new AngularServerMiddleware(next,options).Invoke));
  15. }
  16. }
  17.  
  18. public class AngularServerMiddleware
  19. {
  20. private readonly AngularServerOptions _options;
  21. private readonly AppFunc _next;
  22. private readonly StaticFileMiddleware _innerMiddleware;
  23.  
  24. public AngularServerMiddleware(AppFunc next,AngularServerOptions options)
  25. {
  26. _next = next;
  27. _options = options;
  28.  
  29. _innerMiddleware = new StaticFileMiddleware(_next,options.FileServerOptions.StaticFileOptions);
  30. }
  31.  
  32. public async Task Invoke(IDictionary<string,object> environment)
  33. {
  34. IOwinContext context = new OwinContext(environment);
  35. // try to resolve the request with default static file middleware
  36. await _innerMiddleware.Invoke(environment);
  37. Debug.WriteLine(context.Request.Path + ": " + context.Response.StatusCode);
  38. // *** Right here is where I would expect a 404 but I get a 200 when debugging,// even though my browser eventually returns a 404
  39.  
  40. // route to root path if the status code is 404
  41. // and need support angular html5mode
  42. if (context.Response.StatusCode == 404 && _options.Html5Mode)
  43. {
  44. context.Request.Path = _options.EntryPath;
  45. await _innerMiddleware.Invoke(environment);
  46. Console.WriteLine(">> " + context.Request.Path + ": " + context.Response.StatusCode);
  47. }
  48. }
  49. }
  50. public class AngularServerOptions
  51. {
  52. public FileServerOptions FileServerOptions { get; set; }
  53.  
  54. public PathString EntryPath { get; set; }
  55.  
  56. public bool Html5Mode
  57. {
  58. get
  59. {
  60. return EntryPath.HasValue;
  61. }
  62. }
  63.  
  64. public AngularServerOptions()
  65. {
  66. FileServerOptions = new FileServerOptions();
  67. EntryPath = PathString.Empty;
  68. }
  69. }

解决方法

从您的问题我不确定您是使用IIS还是自我主机.如果您使用的是IIS,那么与使用owin中间件相比,有一个更清洁/更快的解决方案:
您可以使用IIS重写引擎,在Web配置中复制以下内容.
  1. <system.webServer>
  2.  
  3. <rewrite>
  4. <rules>
  5. <!--Redirect selected traffic to index -->
  6. <rule name="Index Rule" stopProcessing="true">
  7. <match url=".*" />
  8. <conditions logicalGrouping="MatchAll">
  9. <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  10. <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
  11. </conditions>
  12. <action type="Rewrite" url="/index.html" />
  13. </rule>
  14. </rules>
  15. </rewrite>
  16. ...
  17. </system.webServer>

此行允许正常提供所有文件

  1. <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

这一行允许api正常提供

  1. <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />

其他一切都得到index.html

猜你在找的asp.Net相关文章