网址帮手完整网址在asp.net MVC-3 [英] Url helper for full url in asp.net mvc-3

查看:117
本文介绍了网址帮手完整网址在asp.net MVC-3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

书写

@Url.Content("~/Something/Something.html")

在剃刀呈现

/AppFolder/Something/Something.html

有没有一种方法来呈现完整的URL http://www.something.com/AppFolder/Something/Something.html 没有穷凶极恶的黑客? (如存储在的AppConfig 的协议和域,将字符串连接到它)

Is there a way to render the full URL like http://www.something.com/AppFolder/Something/Something.html without atrocious hacks? (like storing the protocol and domain in the AppConfig, and concatenate the string to it)

有没有像 @ Url.FullPath(〜/ ASDF / ASDF)或类似的?

推荐答案

在@ Url.RouteURL()不平静的回答了这个问题。它的工作对命名路由,但是未能对任意虚拟路径。
这里是产生全站链接快速的辅助方法。您可以创建各种方案重载(HTTP [S]),这取决于所需的控制程度。

The @Url.RouteURL() does not quiet answer this question. It does work for named routes but falls short for arbitrary virtual paths. Here is quick helper method that generates full outbound url. You can create overloads for various schemes (http[s]) depending on the degree of control desired.

public static class UrlHelperExtension
{
    public static string ContentFullPath(this UrlHelper url,string virtualPath)
    {
        var result = string.Empty;
        Uri requestUrl = url.RequestContext.HttpContext.Request.Url;

        result = string.Format("{0}://{1}{2}",
                               requestUrl.Scheme,
                               requestUrl.Authority, 
                               VirtualPathUtility.ToAbsolute(virtualPath));
        return result;
    }
}

这篇关于网址帮手完整网址在asp.net MVC-3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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