在剃刀使用站点根目录相对链接 [英] Using site root relative links in Razor

查看:136
本文介绍了在剃刀使用站点根目录相对链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,做工精细用剃刀(C#)所有的编码工作正常,当我用我的本地测试(WebMatrix的IIS)。

I have a website that is working fine with Razor (C#) all the coding is working properly when I use my local testing (WebMatrix IIS).

当我把它放在在线我的服务器上的网站是不是在站点的根目录它的自我

When I put it "online" on my server the website is not at the root of the site it self

例如:

HTTP:// intranet.mycompany.com/inform

http:// intranet.mycompany.com/inform

这基本上是我的文件夹结构的根所以我所有的文件夹,从这里开始(CSS文件default.cshtml ...等)

That's basically the "root" of my folder structure so all my folders starts from there (css file default.cshtml... and so on)

我的_PageStart.cshtml看到它正确导致当我从链接 http://intranet.mycompany.com/inform访问我的网站的它给了我,我已在_PageStart.cshtml配置的布局(它确实显示布局+渲染default.cshtml)

My "_PageStart.cshtml" sees it properly cause when I access my site from the link http://intranet.mycompany.com/inform it gives me the Layout I have configured in _PageStart.cshtml (and it really show the layout + the rendered default.cshtml)

但没有别的越来越正确的路径,例如:

BUT nothing else is getting the proper path, for example :

<img src="~/images/logos/hdr.png" />

在IMG持有人那里我可以看到它,但显示该链接被打破......当我右键点击IMG持有人,并做性能上看到的文件应该是它显示我:

The img holder is there I can see it but shows that the link is broken... when I Right-Click the img holder and do properties to see where the files should be it shows me :

HTTP:// intranet.mycompany.com/images/logos/hdr.png

http:// intranet.mycompany.com/images/logos/hdr.png

所以,这将完全没有根相对根...

So it's going to the "full" root not the relative root...

我怎样才能解决呢?

推荐答案

您必须使用在你的应用程序的相对路径:

You have to use relative paths all over your app:

不会静态HTML code内工作。

~ won't work within static html code.

您可以写

<img src="@Url.Content("~/images/logos/hdr.png")" />

<img src="../images/logos/hdr.png" />

第一种方法是很好的布局文件,其中的相对路径可能会改变,当你有不同长度的路由的URL。

The first approach is good for layout files where your relative path might be changing when you have different length routing urls.

修改

至于您关于正常链接的问题:

Regarding to your question about normal links:

当链接到你的应用程序,你不指定视图文件作为目标,但它呈现一个视图作为结果的动作另一页。对于您使用的HtmlHelper ActionLink的

When linking to another page in your app you don't specify the view file as the target but the action which renders a view as the result. For that you use the HtmlHelper ActionLink:

@Html.ActionLink("Linktext", "YourController", "YourAction")

这是自动生成适合你的网址:

That generates the right url for you automatically:

<a href="YourController/YourAction">Linktext</a>

编辑2

好吧,没有MVC - 所以你必须生成你自己的链接

Ok, no MVC - so you have to generate your links yourself.

您必须使用相对路径了。不要启动与 / 字符的任何链接!

You have to use relative paths, too. Don't start any link with the / character!

<a href="linkOnSameLevel.cshtml">Link</a>
<a href="../linkOnParentLevel.cshtml">Link</a>
<a href="subFolder/linkOnOneLevelDown.cshtml">Link</a>

编辑3

在使用布局页,你可以使用 HREF 扩展方法来生成相对URL:

When using Layout pages you can use the Hrefextension method to generate a relative url:

<link href="@Href("~/style.css")" ...

这篇关于在剃刀使用站点根目录相对链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆