Razor 如何在ASP.Net网页中禁用URL路由?

Razor 如何在ASP.Net网页中禁用URL路由?,razor,asp.net-routing,asp.net-webpages,Razor,Asp.net Routing,Asp.net Webpages,或者,如果由于某种原因这是不可能的,一个更简单的问题:如何从布局页面获取当前文件的物理目录 this.VirtualPath将引用布局文件本身的虚拟路径,而this.NormalizePath(“.”)将只获取包含布局文件的目录的虚拟路径 我只是想能够有一个网站,没有可能的相对链接突然不工作,只是因为一些家伙键入没有理由 编辑以向您展示我的意思: TestLayout.cshtml <html> <head> <title>Test Page

或者,如果由于某种原因这是不可能的,一个更简单的问题:如何从布局页面获取当前文件的物理目录

this.VirtualPath
将引用布局文件本身的虚拟路径,而
this.NormalizePath(“.”)将只获取包含布局文件的目录的虚拟路径

我只是想能够有一个网站,没有可能的相对链接突然不工作,只是因为一些家伙键入没有理由

编辑以向您展示我的意思:

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>
@{
   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 />
<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>
@{
    Layout = "TestLayout.cshtml";
}
Inside the main test page!<br />
VirtualPath is @VirtualPath

测试页
@Request.RawUrl
@Request.Url.AbsolutePath
@Request.Url.AbsoluteUri
@Request.Url.LocalPath
@Request.CurrentExecutionFilePath
@请求文件路径
@请求路径
@虚拟路径
@RenderBody()
Test.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>
@{
   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 />
<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>
@{
    Layout = "TestLayout.cshtml";
}
Inside the main test page!<br />
VirtualPath is @VirtualPath
@{
Layout=“TestLayout.cshtml”;
}
@Request.RawUrl
@Request.Url.AbsolutePath
@Request.Url.AbsoluteUri
@Request.Url.LocalPath
@Request.CurrentExecutionFilePath
@请求文件路径
@请求路径
@虚拟路径

如果转到,您会发现正确修剪所有积垢的唯一行是两行
@VirtualPath
行-但是,布局页面的
VirtualPath
属性是TestLayout.cshtml。如何从TestLayout.cshtml中访问Test.cshtml的
VirtualPath
属性?

因此,经过许多、许多小时,我已经找到了答案

不可以,您不能禁用ASP.Net网页的自动URL路由-它被嵌入到
webageroute
类中。禁用它的唯一方法是完全禁用网页

有两种方法可以从子布局访问父.cshtml文件的
VirtualPath

  • 您可以手动解析URL。这是相当复杂的,虽然所有的工作都已经在webageroute.cs文件中完成了,所以您只需稍微调整一下即可。问题是,您只能获取最顶层父页面的VirtualPath
  • 您可以扩展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">
             @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>
    
    @{
       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 />
    
    <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>
    
    @{
        Layout = "TestLayout.cshtml";
    }
    Inside the main test page!<br />
    VirtualPath is @VirtualPath
    
    
    测试页
    在布局页面内
    虚拟路径是:@VirtualPath
    家长的虚拟路径为:@ParentPage.VirtualPath @RenderBody()
    Test.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>
    
    @{
       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 />
    
    <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>
    
    @{
        Layout = "TestLayout.cshtml";
    }
    Inside the main test page!<br />
    VirtualPath is @VirtualPath
    
    @{
    Layout=“TestLayout.cshtml”;
    }
    在主测试页面内
    VirtualPath是@VirtualPath
    输出:

    在布局页面内

    虚拟路径为:~/TestLayout.cshtml

    父级的虚拟路径为:~/Test.cshtml

    在主测试页面内

    VirtualPath是~/Test.cshtml


    使用Request怎么样?如Request.RawUrl或Request.Url.AbsolutePath。我已经尝试了请求对象的所有属性。我将编辑这个问题以向您展示我的意思。您可以添加Test.cshtml的输出作为示例吗?