ASP.NET 5 HTML5历史 [英] ASP.NET 5 HTML5 History

查看:150
本文介绍了ASP.NET 5 HTML5历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm upgrading my project to ASPNET5. My application is a AngularJS Web App that uses HTML5 Url Routing ( HTML5 History API ).

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

In my previous app I used the URL Rewrite IIS Module with code like:

<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中间件来实现这一点。

I realize I could port this but I want to minimize my windows dependencies. From my reading I think I should be able to use ASP.NET 5 Middleware to accomplish this.

我认为代码看起来像这样,但我想我是很远。

I think the code would look something like this but I think I'm pretty far off.

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中实现此目的的最佳方法的任何建议吗?

Essentially, I'm wanting to route anything that contains /api or /signalr. Any suggestions on best way to accomplish this in ASPNET5?

推荐答案

您走在正确的轨道上,而不是发回重定向,我们只想重写Request上的路径。以下代码在ASP.NET5 RC1中起作用。

You were on the right track, but rather than sending back a redirect, we just want to rewrite the path on the Request. The following code is working as of 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();

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

One issue here is that you have to specifically code your angular routes into the middleware (or put them in a config file, etc).

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

Initially, I tried to create a pipeline where after UseDefaultFiles() and UseStaticFiles() had been called, it would check the path, and if the path was not /api, rewrite it and send it back (since anything other than /api should have been handled already). However, I could never get that to work.

这篇关于ASP.NET 5 HTML5历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆