Asp.Net MVC使用Tilde in href属性中的相对路径 [英] Asp.Net MVC using Tilde in href attribute for relative paths

查看:45
本文介绍了Asp.Net MVC使用Tilde in href属性中的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC5和剃须刀.

Using MVC5 and razor.

案例1:

var url='~/path';
<a href='@url'>test</a>

错误,我得到: http://www.example.com/〜/路径

案例#2

var url='/path';
<a href='~/@url'>test</a>

,我得到: http://www.example.com/path

有没有办法让剃刀正确地解释波浪线?换句话说,如果我绝对想像第一种情况那样在字符串中包含代字号,是否有办法使其起作用?

Is there a way to have razor correctly interpret the tilde? in other words, if I absolutely want to include the tilde in a string as in case #1, is there a way to get it to work?

推荐答案

您可以使用 Url.Content()剃刀方法来获取相对路径,如下所示.

You can use Url.Content() razor method to get relative path like following.

@{
  var url="~/path";
}
<a href="@Url.Content(url)">test</a> // you will get http://www.example.com/path

这篇关于Asp.Net MVC使用Tilde in href属性中的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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