简体   繁体   English

在没有MVC的ASP.NET中与Razor的相对链接

[英]Relative links with Razor in ASP.NET without MVC

We're working on a site that was set up with all the pages in the root directory. 我们正在一个使用根目录中所有页面进行设置的网站。 As a result all the links are referencing locations in the same directory they're located in by default... 结果,所有链接都默认引用它们所在的同一目录中的位置。

<a href="Page.cshtml">Page</a> 

We've restructured it and some of the pages are now in sub folders, so the links throughout the site now need to be relative. 我们已经对其进行了重组,并且某些页面现在位于子文件夹中,因此,现在整个站点中的链接都必须是相对的。 I've read that a tilde (~) won't work in the markup when using Razor. 我已经读过使用Razor时波浪号(〜)在标记中不起作用。 ie <a href="~/Page.cshtml">Page</a> <a href="~/Page.cshtml">Page</a>

How can a link be formatted so that it points to something in the root directory? 如何设置链接的格式,使其指向根目录中的某个内容?

Something like? 就像是? <a href="@Href("~/Page.cshtml")">Page</a>

EDIT: Resolved with: <a href="@Href("~/")Page.cshtml">Page</a> 编辑:解析为: <a href="@Href("~/")Page.cshtml">Page</a>

Try this: 尝试这个:

@helper _href(string url)
{
    @VirtualPathUtility.ToAbsolute(url)
}

To use it on a page: 要在页面上使用它:

<a href="@_href("~/page.html")">linky</a>

Edit: How I never knew about the built-in Href method is beyond me. 编辑:我对内置Href方法一无所知。 You should definitely use that instead. 您绝对应该使用它。

Use the Href method, like this: 使用Href方法,如下所示:

@Href("SomePage")

Note that you don't need to use file extensions. 请注意,您不需要使用文件扩展名。

Glance at @Html.ActionLink 一览@ Html.ActionLink

It allows you use controller name 它允许您使用控制器名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM