2011-04-23 4 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 

편집 : 경로 경로가 잘못되어 문제가 발생했습니다. 당신은 Btw은 "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

아 .. 경로 경로가 잘못되었습니다. "게시물/테스트/{제목}/{ID}"로 변경하십시오. – Damb

+0

많은 도움을주었습니다. – Freshblood

관련 문제