加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET 5 HTML5历史

发布时间:2020-12-16 04:28:02 所属栏目:asp.Net 来源:网络整理
导读:我正在将我的项目升级到ASPNET5.我的应用程序是使用 HTML5 URL路由( HTML5 History API)的AngularJS Web App. 在我以前的应用程序中,我使用URL重写IIS模块,代码如下: system.webServer rewrite rules rule name="MainRule" stopProcessing="true" match url
我正在将我的项目升级到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之外的其他内容应该已经处理过了).但是,我永远无法让它发挥作用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读