ASP.NET 5 HTML5历史

我正在将我的项目升级到ASPNET5.我的应用程序是使用 HTML5 URL路由( HTML5 History API)的AngularJS Web App.

在我以前的应用程序中,我使用URL重写IIS模块,代码如下:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="MainRule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          <add input="{REQUEST_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
          <add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
        </conditions>
        <action type="Rewrite" url="Default.cshtml" />
      </rule>
    </rules>
  </rewrite>
<system.webServer>

我意识到我可以移植它,但我想最小化我的Windows依赖项.从我的阅读中我认为我应该能够使用ASP.NET 5中间件来实现这一目标.

我认为代码看起来像这样但我觉得我相当遥远.

app.UseFileServer(new FileServerOptions
{
    EnableDefaultFiles = true,EnableDirectorybrowsing = true
});

app.Use(async (context,next) =>
{
    if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("api"))
    {
        await next();
    }
    else
    {
        var redirect = "http://" + context.Request.Host.Value;// + context.Request.Path.Value;
        context.Response.Redirect(redirect);
    }
});

基本上,我想要路由包含/ api或/ signalr的任何内容.有关在ASPNET5中实现此目的的最佳方法的任何建议吗?

解决方法

你是在正确的轨道上,但我们只想重写请求上的路径,而不是发回重定向.以下代码从ASP.NET5 RC1开始运行.
app.UseIISPlatformHandler();

// This stuff should be routed to angular
var angularRoutes = new[] {"/new","/detail"};

app.Use(async (context,next) =>
{
    // If the request matches one of those paths,change it.
    // This needs to happen before UseDefaultFiles.
    if (context.Request.Path.HasValue &&
        null !=
        angularRoutes.FirstOrDefault(
        (ar) => context.Request.Path.Value.StartsWith(ar,StringComparison.OrdinalIgnoreCase)))
    {
        context.Request.Path = new PathString("/");
    }

    await next();
});

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();

这里的一个问题是你必须专门将角度路由编码到中间件(或将它们放在配置文件中等).

最初,我尝试创建一个管道,在调用UseDefaultFiles()和UseStaticFiles()之后,它会检查路径,如果路径不是/ api,则重写它并将其发回(因为除了/ api之外的其他内容应该已经处理过了).但是,我永远无法让它发挥作用.

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...