2012-07-06 4 views
1

일부 컨트롤러를 통해 요청 된 URL을 처리 할 수 ​​있기를 원합니다. foo.com/a알 수없는 URL에 대한 MVC 3 라우팅

에 대한

foo.com/a 
foo.com/abcd 
foo.com/x1 

나는 Process(string url) 방법

UrlHandlerController으로 처리하고자합니다.

이 작업을 수행하려면 라우팅 규칙을 어떻게 추가해야합니까?

아이디어가 있으십니까?

답변

4

새 사용자 지정 경로를 만들고 경로를 테스트하기 위해 필 Haack의 Route Debugger를 사용

routes.MapRoute(
    "customroute", 
    "{url}", 
    new { controller = "UrlHandler", 
     action = "Process", 
     url = "" 
    } 
); 

컨트롤러 :이 경로 도움이된다면

public class UrlHandlerController : Controller 
{ 
    [HttpGet] 
    public ActionResult Process(string url) 
    { 
     return View(); 

     /* or */ 
     if(url == "something"){ 
      return View("SomethingView"); 
     } 
     else if(url == "somethingelse"){ 
      return View("SomethingElseView"); 
     } 
    } 

} 
+0

왜 url = UrlParameter.Optional입니까? 그것은 선택 사항입니까? – DarthVader

+0

아니요, 간과하지 않습니다. – xandercoded

+0

잘 모르겠지만 404. – DarthVader

0

다스는 다음을 참조하십시오

routes.MapRoute(
    "CustomRoute", // Route name 
    "{url}", //Route formation 
    new { controller = "UrlHandler", action = "Process" }, // Where to send it 
    new { keyWord = @"\S+" } // Regex to identify the argument 
); 

문안 인사.