2016-12-12 13 views
2

asp.net 4.0で、我々は今、どうやってできた私に教えて、このようASP.Net MVC:どのようにミドルウェアでURLを書き換えるASP.NETコアで

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    string CountryCodeInUrl = "", redirectUrl=""; 
    var countryCode = CookieSettings.ReadCookie(); 
    if (countryCode=="") 
    { 
     countryCode = "gb"; 
    } 

    if (HttpContext.Current.Request.RawUrl.Length >= 2) 
    { 
     CountryCodeInUrl = HttpContext.Current.Request.RawUrl.Substring(1, 2); 
    } 

    if (countryCode != CountryCodeInUrl) 
    { 
     if (HttpContext.Current.Request.RawUrl.Length >= 2) 
     { 
      if (HttpContext.Current.Request.RawUrl.Substring(1, 2) != "") 
      { 
       countryCode = HttpContext.Current.Request.RawUrl.Substring(1, 2); 
      } 
     } 
     if(!System.Web.HttpContext.Current.Request.RawUrl.Contains(countryCode)) 
     { 
      redirectUrl = string.Format("/{0}{1}", countryCode, System.Web.HttpContext.Current.Request.RawUrl); 
     } 
     else 
     { 
      redirectUrl = System.Web.HttpContext.Current.Request.RawUrl; 
     } 
     CookieSettings.SaveCookie(countryCode); 
     System.Web.HttpContext.Current.Response.RedirectPermanent(redirectUrl); 
    } 
} 

のようなモジュールを書き換えるために、HTTPモジュールで作業を使用することができます上記のコードをASP.NET Coreのミドルウェアで書き直しますか?

私は部分的にhttps://docs.microsoft.com/en-us/aspnet/core/migration/http-modules

詳細に私を導いてくださいこの記事を読んだことがあります。ありがとう

答えて

2

コードをミドルウェアクラスに移動し、System.Webの代わりにCore HttpContextを使用するだけで十分です。

そのようなクラスは次のようになります。あなたはMVCミドルウェアを登録する前に、あなたが、あなたStartup.csファイルに登録し

//RedirectMiddleware.cs

public class RedirectMiddleware 
{ 
    private readonly RequestDelegate _next; 

    public RedirectMiddleware(RequestDelegate next) 
    { 
     _next = next; 
    } 

    public async Task Invoke(HttpContext context) 
    { 
     string CountryCodeInUrl = "", redirectUrl = ""; 
     var countryCode = CookieSettings.ReadCookie(); 
     if (countryCode == "") 
     { 
      countryCode = "gb"; 
     } 

     if (context.Request.Path.Value.Length >= 2) 
     { 
      CountryCodeInUrl = context.Request.Path.Value.Substring(1, 2); 
     } 

     if (countryCode != CountryCodeInUrl) 
     { 
      if (context.Request.Path.Value.Length >= 2) 
      { 
       if (context.Request.Path.Value.Substring(1, 2) != "") 
       { 
        countryCode = context.Request.Path.Value.Substring(1, 2); 
       } 
      } 
      if (!context.Request.Path.Value.Contains(countryCode)) 
      { 
       redirectUrl = string.Format("/{0}{1}", countryCode, context.Request.Path.Value); 
      } 
      else 
      { 
       redirectUrl = context.Request.Path.Value; 
      } 
      CookieSettings.SaveCookie(countryCode); 
      context.Response.Redirect(redirectUrl, true); 
     } 

     await _next.Invoke(context); 
    } 
} 

がそれを使用するには、このように:

app.UseMiddleware<RedirectMiddleware>(); 

app.UseMvc(routes => 
{ 
    routes.MapRoute(
     name: "default", 
     template: "{controller=Home}/{action=Index}/{id?}"); 
}); 

私はこれがあなたが始めるよ、あなたはミドルウェアの詳細についてはthisブログ投稿を参照してくださいすることができます願っています。

+0

ページはリダイレクトされませんが、リダイレクトされるはずですが、移動しません – 1AmirJalali

関連する問題