2017-07-25 2 views
0

アプリケーションの起動後にルートテーブルを更新しようとしています。私は "ページ"と呼ばれるMVCエリアでこれをやっています。 Webサイトには、各顧客のカスタムURLがあります。www.mydomain.com/Pages/CompanyName/ウェブサイトの開始後にエリアルートを更新するにはどうすればよいですか?

ウェブサイトの起動時に問題なく動作します。これは、すべての既存の顧客のURLをピックアップし、あなたがそれらにナビゲートすることができます

public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     List<Customer> custs = new CustBLL().getActiveCustomers(); 

     string urlList = string.Join("|", custs.Select(c => c.UrlIdentifier).ToArray()); 

     context.MapRoute(
      "Pages_CUSTURL", // Route name 
      "Pages/{CUSTURL}/{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
      constraints: new { CUSTURL = urlList } 
     ); 


     context.MapRoute(
      "Pages_default", 
      "Pages/{controller}/{action}/{id}", 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      constraints: new { controller = new ControllerConstraint() } 
     ); 

    } 

しかし、新しい顧客が作成されたとき、あなたはその顧客のためのURLを取得することができない、「www.mydomain.com/Pages/NewCompany/"、404が発生するためです。

私は新しい顧客が作成された後に呼び出される新しい関数 "UpdateRouteRegistration()"を追加しようとしました。

public static void UpdateRouteRegistration() 
    { 
     RouteCollection routes = RouteTable.Routes; 
     using (routes.GetWriteLock()) 
     { 
      routes.Remove(RouteTable.Routes["Pages_CUSTURL"]); 

      List<Customer> custs = new CustBLL().getActiveCustomers(); 

      string urlList = string.Join("|", custs.Select(c => c.UrlIdentifier).ToArray()); 

      routes.MapRoute(
       "Pages_CUSTURL", // Route name 
       "Pages/{CUSTURL}/{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
       constraints: new { CUSTURL = urlList } 
      ); 

     } 
    } 

新しい機能は動作しません。実行時にエラーはありませんが、その後はどこでもナビゲートできません。ブランクビューエラーが表示されます。ルーティングテーブルが壊れているようです。

'/'アプリケーションでサーバーエラーが発生しました。

ビュー「BlankView」またはそのマスターが見つかりませんでした。または、検索エンジンが検索された場所をサポートしていません。次の場所が検索された:
〜/ビュー/ホーム/ BlankView.aspx
〜/ビュー/ホーム/ BlankView.ascx
〜/ビュー/共有/ BlankView.aspx
〜/ビュー/共有/ BlankView.ascx
〜/ビュー/ホーム/ BlankView.cshtml
〜/ビュー/ホーム/ BlankView.vbhtml
〜/ビュー/共有/ BlankView.cshtml
〜/ビュー/共有/ BlankView.vbhtml
〜/ __ MVCSITEMAPPROVIDER/BlankView.ascx

MVC 5.2.3を使用しています。どんな助けでも大歓迎です。

編集:AreaRegistraitionContextを使用する必要があるのだろうかと思います。しかし、この投稿から、私はリフレクションを使ってアクセスすることができますか? Get Areas associated with an MVC project

答えて

1

私はこれを有効にして、同じことをしている可能性のある人と分かち合いたいと思っていました。これが最善の方法であるかどうかはわかりませんが、うまくいきます。

私は2つのことをしました。 1つは、後で使用するためにAreaRegistrationContextを静的メンバーに保存しました。 2つ、私は既存のマッピングを追加/削除しようとしませんでした。代わりに、私はちょうど新しいものを追加しました。

私の場合、新しい顧客はあまり頻繁には追加されないため、この領域にはあまりにも多くのマッピングがありません。

ここは最終的なクラスです。

public class PagesAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "Pages"; 
     } 
    } 

    public static AreaRegistrationContext AreaContext { get; set; } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 

     context.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     // save context to static for later use 
     AreaContext = context; 

     // get customers 
     List<Customer> custs = new CustBLL().getActiveCustomers(); 

     // get list of domain urls 
     string urlList = string.Join("|", custs.Select(c => c.UrlIdentifier).ToArray()); 

     // map customer URLs 
     context.MapRoute(
      "Pages_CUSTURL", // Route name 
      "Pages/{CUSTURL}/{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
      constraints: new { CUSTURL = urlList } 
     ); 

     // general area mapping 
     context.MapRoute(
      "Pages_default", 
      "Pages/{controller}/{action}/{id}", 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      constraints: new { controller = new ControllerConstraint() } 
     ); 

    } 

    public static void UpdateRouteRegistration(string newURLID) 
    { 
     // get context from static member 
     RouteCollection routes = AreaContext.Routes; 

     // get write lock to avoid threading issues 
     using (routes.GetWriteLock()) 
     { 

      // add new company url to route table 
      AreaContext.MapRoute(
       "Pages_CUSTURL_" + newURLID,          // Route name 
       "Pages/{CUSTURL}/{controller}/{action}/{id}",      // URL with parameters 
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults 
       constraints: new { CUSTURL = newURLID } 
      ); 

     } 

    } 


} 

MVCエリアルーティングでこのようなことを達成しようとしている場合は、幸いです!

関連する問題