Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 495 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Url helper for full url in asp.net mvc-3

#1
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?
Reply

#2
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;
}
}
Reply

#3
See [this blog post][1] for the answer.

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

Url.Action("About", "Home", null, "http")

[1]:

[To see links please register here]

Reply

#4
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>;
Reply

#5
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`
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through