2014-06-13 3 views
0

또는 어떤 이유로 든 불가능하다면 더 간단한 질문입니다. 레이아웃 페이지에서 현재 파일의 실제 디렉토리를 얻으려면 어떻게해야합니까?ASP.Net 웹 페이지에서 URL 라우팅을 비활성화하려면 어떻게합니까?

this.VirtualPath은 레이아웃 파일 자체의 가상 경로를 나타내며 this.NormalizePath(".")은 레이아웃 파일이있는 디렉토리의 가상 경로 만 가져옵니다.

그냥 어떤 사람이 아무 이유없이 http://example.com/index/some/other/junk을 입력했기 때문에 갑자기 작동하지 않는 상대 링크의 가능성이없는 사이트를 가질 수 있기를 바랍니다.

편집은 내가 무슨 뜻인지를 보여 :

TestLayout.cshtml을

<html> 
    <head> 
     <title>Test Page</title> 
    </head> 
    <body> 
     <div style="background: grey"> 
     @Request.RawUrl<br /> 
     @Request.Url.AbsolutePath<br /> 
     @Request.Url.AbsoluteUri<br /> 
     @Request.Url.LocalPath<br /> 
     @Request.CurrentExecutionFilePath<br /> 
     @Request.FilePath<br /> 
     @Request.Path<br /> 
     @VirtualPath<br /> 
     </div> 
     <div style="background: red"> 
     @RenderBody() 
     </div> 
    </body> 
</html> 

Test.cshtml

@{ 
    Layout = "TestLayout.cshtml"; 
} 
@Request.RawUrl<br /> 
@Request.Url.AbsolutePath<br /> 
@Request.Url.AbsoluteUri<br /> 
@Request.Url.LocalPath<br /> 
@Request.CurrentExecutionFilePath<br /> 
@Request.FilePath<br /> 
@Request.Path<br /> 
@VirtualPath<br /> 

당신이 http://example.com/Test/random/junk/at/the/end에 가면, 당신은 찾을 수 있습니다 모든 찌그러 뜨림을 정확하게 다듬는 유일한 선은 두 가지입니다. @VirtualPath 행 - 그러나 레이아웃 페이지의 VirtualPath 속성은 TestLayout.cshtml입니다. TestLayout.cshtml에서 Test.cshtml의 VirtualPath 속성에 액세스하려면 어떻게해야합니까?

+0

에게 무엇입니까? Request.RawUrl 또는 Request.Url.AbsolutePath와 같습니다. http://stackoverflow.com/a/16693496/858905 – johna

+0

기본적으로 Request 개체의 모든 속성을 시도했습니다. 내가 의미하는 바를 보여주기 위해 질문을 편집 할 것이다. – Santiclause

+0

Test.cshtml의 출력을 예제로 추가 할 수 있습니까? – johna

답변

0

많은 시간이 지난 후에, 나는 그것을 알아 냈습니다.

아니요, ASP.Net 웹 페이지의 자동 URL 라우팅을 사용 중지 할 수 없습니다. WebPageRoute 클래스로 구워졌습니다. 이를 비활성화하는 유일한 방법은 웹 페이지를 완전히 비활성화하는 것입니다.

하위 레이아웃에서 상위 .cshtml 파일의 VirtualPath에 액세스 할 수있는 두 가지 방법이 있습니다.

  1. URL을 수동으로 구문 분석 할 수 있습니다. 이것은 모든 작업이 이미 WebPageRoute.cs 파일에서 수행되었지만 상당히 복잡합니다. 따라서 몇 가지 조정을 통해이 파일을 닉크로 만들 수 있습니다. 문제는 최상위 부모 페이지의 VirtualPath 만 가져올 수 있다는 것입니다.
  2. WebPage 클래스를 확장하고 ConfigurePage 메서드를 재정의하고 나중에 사용할 수 있도록 인수를 저장할 수 있습니다. 예시적인 코드가 다음과

CustomWebPage.cs

using System.Web; 
using System.Web.WebPages; 
public abstract class CustomWebPage : WebPage 
{ 
    private WebPageBase _parentPage; 

    public WebPageBase ParentPage { get; private set; } 

    protected override void ConfigurePage(WebPageBase parentPage) 
    { 
     ParentPage = parentPage; 
    } 
} 

TestLayout.cshtml

<html> 
    <head> 
     <title>Test Page</title> 
    </head> 
    <body> 
     <div style="background: grey"> 
     Inside the layout page!<br/> 
     VirtualPath is: @VirtualPath<br /> 
     Parent's VirtualPath is: @ParentPage.VirtualPath 
     </div> 
     <div style="background: red"> 
     @RenderBody() 
     </div> 
    </body> 
</html> 

Test.cshtml

@{ 
    Layout = "TestLayout.cshtml"; 
} 
Inside the main test page!<br /> 
VirtualPath is @VirtualPath 

출력 : 레이아웃 페이지 내부

!

VirtualPath는 ~/TestLayout입니다.cshtml

부모의 VirtualPath은 다음과 같습니다 ~/Test.cshtml

주요 테스트 페이지 내부

!

VirtualPath 어떻게 요청을 사용하는 방법에 대한 ~/Test.cshtml

관련 문제