ASP.NET 5 HTML5历史

前端之家收集整理的这篇文章主要介绍了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之外的其他内容应该已经处理过了).但是,我永远无法让它发挥作用.

原文链接:https://www.f2er.com/aspnet/248373.html

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