2016-07-19 5 views
0

ユーザーがプロジェクト情報を追加できるシステムを作成しています。システムの一部には、プロジェクトに掛かっているサブフォームがあります。イムはどのようにルーティングを設定するかについて私の頭を上げようとしており、以下のようなルートを作ることが可能かどうか疑問に思っています。カスタムルートをmvcプロジェクトに追加する

/project/ 
/project/12345 
/project/12345/bid - id is the same as the project e.g 12345 
/project/12345/bid/create - id is the same as the project e.g 12345 
/project/12345/bid/edit - id is the same as the project e.g 12345 

これは良いアイデアですか

/bid 
/bid/create - id is the same as the project e.g 12345 
/bid/12345 - id is the same as the project e.g 12345 
/bid/12345/edit - id is the same as the project e.g 12345 

答えて

0

ない大きな違いを行う方が良いでしょう。しかし、私はあなたに最初のバージョンを使用することをお勧めします。他にも/projectが表示されることがあります。また、そのようなURLはより明白で自己記述的です。

これを実装するには、attribute routingをご覧ください。それはあなたのニーズを完全に満たすでしょう。

例:

[RoutePrefix("project")] 
public class ProjectController : Controller 
{ 
    // eg: /project 
    [Route("")] 
    public ActionResult Index() { ... } 

    // eg: /project/1234 
    [Route("{projectID:int}")] 
    public ActionResult Project(int projectID) { ... } 

    // eg: /project/1234/bid 
    [Route("{projectID:int}/bid")] 
    public ActionResult Bid(int projectID) { ... } 

    // eg: /project/1234/bid/create 
    [Route("{projectID:int}/bid/create")] 
    Public ActionResult CreateBid(int projectID) { ... } 

    // so on 
} 

そして、このようなルートを登録することを忘れないでください:それは公共のウェブサイトでない限り

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    // other routes if you need 

    routes.MapMvcAttributeRoutes(); 
} 
0

は、私は次のようなもののために行くだろうし、私は中に小さな説明を追加しますSEOのURL。

/入札/ 789

/入札/作成

/入札/編集/ 12345

routes.MapRoute(
     name: "Default", 
     url: "{controller}/{id}/{action}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 
関連する問題