2009-09-05 4 views
0

nullable int를 사용하고 'dosomething.aspx'보기를 반환하는 'dosomething'이라는 컨트롤러 (컨트롤러 이름은 'makemagic')에 대한 액션이 있습니다. 적어도 이것이 내가하려는 일입니다. Default() 뷰로 라우팅 되더라도 상관 없습니다.ASP.NET MVC 리디렉션 작업을 기본값으로 설정 하시겠습니까?

public ActionResult dosomething(int? id) 
{  
    var model = // business logic here to fetch model from DB 
    return View("dosomething", model); 
} 

상속 System.Web.Mvc.ViewPage

이있는 /Views/makemagic/dosomething.aspx 파일이 있습니다 내 경로에 뭔가를해야합니까? 내 global.aspx.cs 파일에 '재고'기본 경로가 있습니다.

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

     routes.MapRoute(
      "Default",            // Route name 
      "{controller}/{action}/{id}",       // URL with parameters 
      new { controller = "Home", action = "Index", id = "" } // Parameter defaults 
    ); 

} 

다른 페이지에서 이와 같이 href를 통해 작업을 호출합니다.

<a href="/makemagic/dosomething/25">Click Me!</a> 

심각하게 날 너트를 몰고. 이 문제를 해결하는 방법에 대한 제안 사항이 있으십니까? 나는 내 경로 정의에서 break를 디버그하려고 시도했는데, 예상했던대로 break가 발생하지 않는 것 같습니다.

답변

2

매개 변수가 nullable이 아니므로 변경하여 기본 경로와 일치 시키거나 이름을 id가 아닌 다른 것으로 변경하고 쿼리 매개 변수로 제공하십시오. 후자의 예는 다음과 같습니다.

public ActionResult dosomething(int? foo) 
{  
    var model = // business logic here to fetch model from DB 
    return View("dosomething", model); 
} 

<a href="/makemagic/dosomething?foo=25">Click me</a> 

기본 라우팅 구현과 함께 작동합니다. 또는 기본 경로와 구별 할 수있는 작업을 수행 한 다음 쿼리 매개 변수를 사용할 필요가없는 경로를 사용할 수 있습니다.

routes.MapRoute(
     "Default",          // Route name 
     "{controller}/{action}/foo/{id}",    // URL with parameters 
     new { controller = "makemagic", action = "dosomething", id = "" } // Parameter defaults 
); 

<a href="/makemagic/dosomething/foo/25">Click Me!</a> 
+0

답장을 보내 주셔서 감사합니다. 나는 방금 무언가를 놓친 것 같아요 : 같은 행동; ActionResult dosomething (int foo) {} 시도했지만 실패했습니다. Click 시도해 보았습니다. 실패했습니다. Click 나를/makemagic/details 액션으로 보냅니다!?!?! 새 경로를 설정해야한다면 이상하게 보입니다.이 경로는 일반적인 시나리오입니까? 예? – CmdrTallen

+0

<% = Html.ActionLink ("makemagic", "dosomething", new {foo = 25}) %> 시도도 실패했습니다. Grrrr. – CmdrTallen

+0

URL을 복사하여 새 창에 붙여 넣으면 작동하지만 페이지에서 '잃어버린'것으로 보입니다. 붙여 넣을 때 브라우저에서 작동합니다. http : // localhost : port/makemagic/dosomething/foo/25 하지만 href로 가면 보인다. http : // localhost : port/home/default 아이디어가 있습니까? – CmdrTallen

관련 문제