2014-11-10 4 views
0

최근에 웹 사이트를 AJAX에서 ASP.MVC5로 업데이트 했으므로 여전히 검색 엔진에있는 이전 URL을 제거해야합니다. 나의 오래된 URL이 AJAX를 기반으로하기 때문에 모든 이전 URL을 홈페이지에-직접 다시 때문에401 HTTP 오류 상태 코드가 반환됩니다.

, 구글은 거 예를 들어, 자기를이 제거되지 않습니다 :

mysite.com/#!product/phone/samsung/galaxy3 
mysite.com/#!product/phone/iphone/ip4s 
mysite.com/#!product/phone/nokia/lumia920 

지금이 솔루션은 구글로 돌아입니다 401 개 HTTP 오류 매번 상태 서버는 이전 URL의 요청 내가 다시 MVC5에 적극적으로 requester401 HTTP 오류 상태 코드를 반환하려면 어떻게

을 (#! 또는 _escaped_fragment_ 포함)를 수신? 고마워!

답변

1

경로를 작성해야합니다. 경로에서 RedirectLocation를 작성하고 방금 이전 URL을 확인할 수 있습니다 컨트롤러에이 달성하고자하는 간단하게 할 경우 Status

public class NewUrlRoute : RouteBase 
     { 
     public override RouteData GetRouteData(HttpContextBase httpContext) 
     { 
      const string status = "401 HTTP error status"; 
      var request = httpContext.Request; 
      var response = httpContext.Response; 
      var title = ""; 
      var legacyUrl = request.Url.ToString(); 
      var newUrl = ""; 
      var id = request.QueryString.Count != 0 ? request.QueryString[0] : ""; 

      if (legacyUrl.Contains(" #!")) 
      { 
      response.Status = status; 
      response.RedirectLocation = "newUrl"; 
      response.End(); 
      } 
      return null; 
     } 

     } 
+0

더 자세히 설명해 주시겠습니까? 처음 URL을 받았을 때 모든 URL을 처리하도록하려면 어떻게해야합니까? – NeedAnswers

+0

다음 코드를 볼 수 있습니다 : http://www.mikesdotnetting.com/article/108/handling-legacy-urls-with-asp-net-mvc –

1

을 반환 :

HttpContext.Response.StatusCode = 401; 

라우팅 엔진이 자동으로 처리합니다 응답 헤더에 관계없이.

+0

이 코드를 임베드하는 대신 상태 코드를 확인하고 전역으로 반환하는 방법 모든 컨트롤러? – NeedAnswers

+0

@hoangnnm 당신이 그것을 성취 할 수있는 몇 가지 방법이 있습니다. 기본 컨트롤러를 구현하고 컨트롤러 요청 파이프 라인 메서드 중 하나를 재정의 할 수 있습니다. http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.aspx (BeginExecute, Ecexute 등)를 보거나 routedata에 액세스 할 수있는 사용자 정의 컨트롤러 팩토리를 볼 수 있습니다. 응답을 렌더링합니다. 그러나 Dimitry가 제안한 것과 비슷한 커스텀 라우트 핸들러를 구현할 것입니다. 당신은 좋은 예제를 여기에서 볼 수있다 http://www.codeproject.com/Articles/595520/MvcRouteHandler-and-MvcHandler-in-ASP-NET-MVC-Fram – geekonedge

+0

고마워, 내가 해결 했어. – NeedAnswers

0

가 해결, 아주 쉬운 방법이 밝혀졌다 (아래 코드는 .NET MVC-4 routing with custom slugs이 방법으로

protected override IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
     var url = requestContext.HttpContext.Request.Path.TrimStart('/'); 


     if (!string.IsNullOrEmpty(url)) 
     { 
      if (url.Contains("_escaped_fragment_")) 
       requestContext.HttpContext.Response.StatusCode = 401; 
      else 
       requestContext.HttpContext.Response.StatusCode = 404; 

      FillRequest("Error","Index", requestContext); 
     } 

     return base.GetHttpHandler(requestContext); 
    } 

, 나는 심지어 내가 이전에 어떤 lucks없이 고투 404 custom error page을 구현할 수가 작동하지되면, 이상) :

 // Add this code to handle non-existing urls 
     routes.MapRoute(
      name: "404-PageNotFound", 
      // This will handle any non-existing urls 
      url: "{*url}", 
      // "Shared" is the name of your error controller, and "Error" is the action/page 
      // that handles all your custom errors 
      defaults: new { controller = "Error", action = "Index" } 
관련 문제