当前位置: 移动技术网 > IT编程>开发语言>.net > [译]ASP.NET Core 2.0 网址重定向的方法

[译]ASP.NET Core 2.0 网址重定向的方法

2017年11月02日  | 移动技术网IT编程  | 我要评论

广东省考试院网,朴翔 帕尔哈提,巅峰高手的暧昧人生

问题

如何在asp.net core 2.0中实现网址重定向?

答案

新建一个空项目,在startup.cs文件中,配置rewriteoptions参数并添加网址重定向中间件(userewriter):

public void configure(iapplicationbuilder app, ihostingenvironment env)

{

  var rewrite = new rewriteoptions()

    .addredirect("films", "movies")

    .addrewrite("actors", "stars", true);

 

  app.userewriter(rewrite);

 

  app.run(async (context) =>

  {

    var path = context.request.path;

    var query = context.request.querystring;

    await context.response.writeasync($"new url: {path}{query}");

  });

} 

运行,并在浏览器地址栏输入:http://www.lhsxpumps.com/_localhost:56825/films,通过客户端调试工具观察重定向过程:

在地址栏输入:http://www.lhsxpumps.com/_localhost:56825/actors,再次观察重定向过程:

讨论

网址重定向就是根据用户自定义规则来修改请求的网址,目的是为了将服务器资源和浏览器网址解绑定。这样做可能是出于安全考虑, 搜索引擎优化(seo),用户友好网址,将http重定向到https等多种目的。

当你无法使用web服务器(iis,apache,nginx)的重定向功能时,asp.net core提供了一个可选项 - 请求网址重定向中间件。然后它的性能和功能比不上web服务器的重定向。

重定向中间件可以做两件事情:客户端重定向和服务器重写:

重定向(客户端)

这是一个客户端操作,工作流程如下:

1. 客户端请求一个资源,比如 /films

2. 服务器返回301(moved permanently)或者302(found)状态码,并在响应头中添加location属性,用来指示浏览器请求新的地址(比如/movies)。

3. 客户端请求新的地址,并显示在浏览器的地址栏中。

重写(服务端)

它是一个服务器端操作,工作流程如下:

1. 客户端请求一个资源,比如 /actors

2. 服务器将其内部映射到新的地址(比如/stars)并且返回200(ok)。

在此过程中,客户端并不知道服务器端的内部映射操作,因此用户看到的浏览器地址栏依然显示的是最初请求地址。

规则

重定向和重写规则可以是正则表达式,更加详细的信息请参考:

自定义重定向规则

我们也可以自定义重定向规则,通过一个继承自irule接口的类来实现:

public class moviesredirectrule : irule

{

  private readonly string[] _matchpaths;

  private readonly string _newpath;

 

  public moviesredirectrule(string[] matchpaths, string newpath)

  {

    _matchpaths = matchpaths;

    _newpath = newpath;

  }

 

  public void applyrule(rewritecontext context)

  {

    var request = context.httpcontext.request;

 

    // 已经是目标地址了,直接返回

    if (request.path.startswithsegments(new pathstring(_newpath)))

    {

      return;

    }

 

    if (_matchpaths.contains(request.path.value))

    {

      var newlocation = $"{_newpath}{request.querystring}";

 

      var response = context.httpcontext.response;

      response.statuscode = statuscodes.status302found;

      context.result = ruleresult.endresponse;

      response.headers[headernames.location] = newlocation;

    }

  }

}   

然后在configure()中,将此自定义规则添加到rewriteoptions里面:

public void configure(iapplicationbuilder app, ihostingenvironment env)

{

  var rewrite = new rewriteoptions()

    .add(new moviesredirectrule(

      matchpaths: new string[] { "/films", "/features", "/albums" },

      newpath: "/movies"));

 

 

  app.userewriter(rewrite);

 

  app.run(async (context) =>

  {

    var path = context.request.path;

    var query = context.request.querystring;

    await context.response.writeasync($"new url: {path}{query}");

  });

}   

运行,在地址栏输入:http://www.lhsxpumps.com/_localhost:56825/films?id=123,观察重定向过程:

原文:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网