2011-04-23 6 views
0
public class PostController : YourDefinitionController 
{ 
    public ActionResult Test(int id ,int title) 
    { 
     return View(); 
    } 
} 

@Html.ActionLink("Postss", "Test","Post" ,new { title = "asdf",id=3 },null)//in Razor view 

// here is route registration 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapRoute(
     "Post", 
     "{Post}/{Test}/{title}/{id}", 
     new { controller = "Post", action = "Test",id=UrlParameter.Optional, title=UrlParameter.Optional } 
     ); 
    routes.MapRoute(
     "Defaultx", // Route name 
     "{controller}/{action}/{id}", // URL with parameters 
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
    ); 

私は/ Post/Test/asdf/3のようなリンクを期待していましたが、/ Post/Test/3ですか?title = 3 なぜですか?どうすれば修正できますか?異なるルートでアクションリンクを作成しますか?

答えて

1

慣習に基づいて多くのことが行われているため、コードを少し掃除することをおすすめします。だからコードを一貫して保つことはしばしば役に立ちます

public ActionResult Test(string title ,int id) // Order is switched and title changed to string 

編集:問題は経路のパスが間違っていることです。あなたはところで"Post/Test/{title}/{id}"

// changed route path expression 
routes.MapRoute(
    "Post", 
    "Post/Test/{title}/{id}", 
    new { controller = "Post", action = "Test", title = UrlParameter.Optional, id = UrlParameter.Optional } 
); 

にそれを変更する必要があります:あなたはもう少しルートでプレイしようとしている場合、Phil Haack's blogは素晴らしいリソースになります。

+0

同じ結果:( – Freshblood

+1

あなたはあなたのルートパスが間違っています。 "Post/Test/{title}/{id}"に変更してください。 – Damb

+0

ありがとうございましたあなたは助けてくれました – Freshblood

関連する問題