2010-11-18 8 views
2

생성 된 URL에서 ID 필드를 제외 할 수 있지만 여전히 ID 값을 사용할 수 있습니까? 당신이 StackOverflow의 URL을 가지고가는 경우 :라우팅 및 URL 구조

http://stackoverflow.com/questions/3409196/asp-net-mvc-routing-question

해당 URL은 질문 ID없이 렌더링 할 수 있습니까?

답변

1

ID없이 질문을 표시 할 수 있지만 질문 제목은 모든 질문마다 고유해야합니다.

또한 ID를 사용하여 질문을 찾은 다음 질문 제목 만 표시하는 다른 URL로 리디렉션 할 수 있습니다. 그것이 당신이 그것을하고 싶은 방법 인 경우에 나는보기를 게시 할 수있다.

// this method finds a file from database using the id 
//and passes the object with TempData 
    public ActionResult InitialDetail(int id) 
    { 
     var question = questionRepository.GetFile(id); 
     if (question==null) 
      return View("NotFound"); 
     else 
     { 
      TempData["question"] = question; 
      return Redirect("https://stackoverflow.com/questions/" + question.Name); 
     } 
    } 


//this method uses model passed from other method and displays it 
    public ActionResult Details(string questionName) 
    { 
     if (TempData["question"] == null) 
     { 
      return View("NotFound"); 
     } 
     else 
      return View("Details", TempData["question"]); 
    } 

당신은이에 대한 경로를 정의 할 필요가

routes.MapRoute("QuestionPage", //Files/id/fileName 
      "questions/{questionName}", 
      new { controller = "Questions", action = "Details" }); 

그냥 기본 경로 전에이 경로 추가 작업 :

다음은 예입니다. URL이 http://domain.com/questions으로 시작하는 경로가 있으면 문제가 생길 수 있습니다.

참고 : 이것은 최상의 솔루션이 아닐 수 있습니다. 질문 제목이 고유하지 않은 경우 페이지에이 구조의 링크를 넣을 수 없습니다. 먼저 ID를 사용하여 질문을 찾아야합니다.

+0

은 리디렉션됩니다. 기본적으로 다른보기로보기를 전달할 수 있습니까? – fr3dr1k8009

+0

잘 모르겠 음 –

+0

오해가 아니라면 리디렉션은 URL을 복사하여 다른 사람에게 붙여 넣을 수 없으므로 악용 사례이므로 같은 페이지를 볼 수 있습니다 (복사/붙여 넣기에 TempData). 검색 엔진이이 문제를 어떻게 처리할지 모릅니다. – KallDrexx