Or, if that's impossible for some reason, a simpler question: how can I get the physical directory of the current file from a layout page?
this.VirtualPath
will refer to the virtual path of the layout file itself, and this.NormalizePath(".")
will only get the virtual path of the directory containing the layout file.
I just want to be able to have a site that doesn't have the possibility of relative links suddenly not working just because some guy typed http://example.com/index/some/other/junk for no reason.
EDIT to show you what I mean:
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 />
If you go to http://example.com/Test/random/junk/at/the/end, you'll find that the only lines that correctly trim all the cruft are the two @VirtualPath
lines - however, the VirtualPath
property of the layout page is TestLayout.cshtml. How do I access Test.cshtml's VirtualPath
property from within TestLayout.cshtml?