2012-01-06 5 views
0

私はasp.net Webアプリケーションを持っており、今すぐユーザーはwww.webdomain.com/page.aspx?usename=myusernameを入力してプロファイルを取得できます。私はwww.webdomain.com/usernameに変更したいと思います。助けてくれてありがとう。ユーザー名だけでasp.netでURL書き換えを行う方法

+0

あなたが探している言葉は「リダイレクト」だと思います。 –

答えて

2

サンプルIRouteConstraint:

public class IsUserActionConstraint : IRouteConstraint 
{ 
    //This is a static variable that handles the list of users 
    private static List<string> _users; 


    //This constructor loads the list of users on the first call 
    public IsUserActionConstraint() 
    { 
     _users= (from u in Models.Users.Get() select u.Username.ToLower()).ToList(); 
    } 


    //Code for checking to see if the route is a username 
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
    { 
     return _users.Contains((values["username"] as string).ToLower()); 
    } 


} 

そして、Global.asaxの中にルートを登録するには:私の場合は

routes.MapRoute(
      "User Profile", // Route name 
      "{username}", 
      new { controller = "User", action = "Index", id = UrlParameter.Optional },// This stuff is here for ASP.NET MVC 
    new { IsUserAction = new IsUserActionConstraint() } //Your IRouteconstraint 
     ); 

、私のユーザーリストは私ができるので、アプリケーションのライフサイクル中に変化することはありません静的リストを使用してキャッシュします。私はあなたが入力した値が一致の内部のユーザー名であることを確認するために何をチェックしているようにコードを修正することをお勧めします。

+0

私はこれをasp.netに変換する際に問題があるため、これをまだテストしていません new {IsUserAction = new IsUserActionConstraint()} – user516883

+0

あなたのルートを登録するために使用している完全なコードブロックを投稿します。あなたは何か助けてください。 new {}は匿名のオブジェクトまたは型で、.NET 3.0ではこの機能が導入されました。左側は変数名で、右側はクラスのインスタンスを作成しています。クラスファイルの配置場所に応じて、新しいPath.To.Class.IsUserActionConstraint()のようにクラスの名前空間を調整する必要があります。 –

+0

asp.net 4.0を使用する。そのパラメータはbool checkphysicalUrlAddressを期待しています: routeCollection.MapPageRoute( "RouteForUser"、 "{ユーザー名}"、 "〜/ Userprofile.aspx"、新しいIsUserActionConstraint()); – user516883

1
rewriterule www.webdomain.com/.+? www.webdomain.com/page.aspx?usename=$1 
2

MVCルーティングを使用します。これは、WebフォームとMVCのルーティングを使用する方法についての良い記事です:

http://msdn.microsoft.com/en-us/magazine/dd347546.aspx

http://msdn.microsoft.com/en-us/library/cc668177.aspx

+0

良い例。私は今www.example.com/u/usernameを行うことができます。どうすれば/ uを取り除き、/ usernameを持っていれば、リルートは/ – user516883

+0

で始まらないと言います。 "{ユーザー名}"のルートを登録する必要があります。ルートを強制するIRouteConstraintを作成することをお勧めします入力された値がユーザー名だった場合にのみ一致します。 http://stackoverflow.com/questions/4988138/how-to-make-irouteconstraint-filter-routeまたはhttp://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tipを参照してください。 -30-create-custom-route-constraints.aspx –

+0

私にasp.netフォームの例を表示することはできますか?私はIRouteConstraintクラスを持っていますが、どのように使用するのかは分かりません。ありがとう – user516883

0

それを行うには、いくつかの方法があります。 ASP.NET MVCを使用し、IRouteConstraintを使用してルートを作成して、ユーザー名が存在することを確認することができます。

また、www.webdomain.com/usernameのApplication_BeginRequestとHandelリクエストを取得し、ReWritingまたはTransferRequestをwww.webdomain.com/page.aspx?usename=myusernameに転送するIHttpModuleを作成することもできます。

また、IHttpModuleと同じ方法で、Global.asaxで直接コードを実行することもできます。

関連する問題