Url helper for full url in asp.net mvc-3
Asked Answered
B

4

24

Writing

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

in razor renders

/AppFolder/Something/Something.html

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)

Is there a helper like @Url.FullPath("~/asdf/asdf") or similar?

Blearyeyed answered 17/8, 2011 at 8:37 Comment(2)
Did you find an answer for this? I'm looking for the same thing! Any help appreciated.Burrill
Any suggestions to use Url.Action in Mono? #30498400Mobilize
J
23

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;
    }
}
Jerome answered 8/11, 2011 at 9:19 Comment(1)
That was an old question! You might get a badge for this :DBlearyeyed
B
26

See this blog post for the answer.

Basically, all you need to do it include the protocol parameter e.g.

Url.Action("About", "Home", null, "http")
Burrill answered 11/10, 2011 at 19:30 Comment(1)
This is a better answer than using String.Format. +1Scoundrel
J
23

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;
    }
}
Jerome answered 8/11, 2011 at 9:19 Comment(1)
That was an old question! You might get a badge for this :DBlearyeyed
M
0

For anyone needing to build URLs in WebAPI 2.2 and/or MVC5, this worked for me:

// works in a controller
var requestUri = this.Request.RequestUri;
// just the http/s and the hostname; ymmv
string baseUrl = requestUri.Scheme + "://" + requestUri.Authority + "/";
// build your url for whatever purpose you need it for
string url = baseUrl + "SomeOtherController?id=" + <some_magic_value>;
Myriagram answered 3/7, 2017 at 11:22 Comment(0)
S
0

You can use a helper to produce a full url, including protocol. Note the first lowercase in url.Action.

var url = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
var fullUrl = url.Action("YourAction", "YourController", new { id = something }, protocol: System.Web.HttpContext.Current.Request.Url.Scheme);

Output

https://www.yourdomain.com/YourController/YourAction?id=something

Scrivenor answered 11/4, 2019 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.