2011-08-31 29 views
1

是否有一种使用ASP.NET MVC3在页面上显示URL的优雅方式。我在寻找的方法应该接受contollerName,actionName和routeValue(类似于@ Html.ActionLink()通过参数)在页面上显示URL的优雅方式

egCode:这里的链接:@thatMethod( “秀”, “后”,新{邮政= 0})

egResult:这里的链接:http://localhost:3120/Post/Show/0

编辑1: 我不想成为超链接,只是纯文本。

编辑2: 它需要显示域( “HTTP://本地主机:3120”,在前面的例子中)

+0

提供我正确地理解你的需要,你应该检查了这一点:http://urlrewriter.net/ – Kheldar

回答

0

This是一个更好的解决方案。

+0

不,我不希望它作为超链接,忘了mention.Thx求助 – kr85

+0

修改我的答案 –

+0

我尝试了正如你所说的,结果我得到了“/ Post/Show/0”,但我想得到“http:// localhost:3120/Post/Show/0”。任何想法? – kr85

0

您可以使用Html Helper扩展。

HTML辅助类

public static class HtmlHelpersExtensions 
{ 
    public static string ActionLinkText(this HtmlHelper helper, string linkText, string actionName, string controllerName,object htmlAttributes, string spanAttributes) 
    { 
    TagBuilder spanBuilder = new TagBuilder("span"); 
    spanBuilder.InnerHtml = linkText; 
    spanBuilder.Attributes.Add("class", spanAttributes); 

    return BuildAnchor(spanBuilder.ToString(), string.Format("/{0}/{1}", controllerName, actionName), htmlAttributes); 
    } 

    private static string BuildAnchor(string innerHtml, string url, object htmlAttributes) 
    { 
    TagBuilder anchorBuilder = new TagBuilder("a"); 
    anchorBuilder.Attributes.Add("href", url); 
    anchorBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 
    anchorBuilder.InnerHtml = innerHtml; 

    return anchorBuilder.ToString(); 
    } 
} 

查看

@Html.ActionLinkText("Your Text", "Show", "Post", new { @title = "Your Text" }, "").Raw() 

这可能会帮助你。