How can I quickly determine what the root URL is for my ASP.NET MVC application? I.e., if IIS is set to serve my application at http://example.com/foo/bar, then I'd like to be able to get that URL in a reliable way that doesn't involve getting the current URL from the request and chopping it up in some fragile way that breaks if I re-route my action.
The reason that I need the base URL is that this web application calls another one that needs the root to the caller web application for callback purposes.
Assuming you have a Request object available, you can use:
string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
If it's not available, you can get to it via the context:
var request = HttpContext.Current.Request
So none of the ones listed here worked for me, but using a few of the answers, I got something working:
public string GetBaseUrl()
{
var request = HttpContext.Current.Request;
var appUrl = HttpRuntime.AppDomainAppVirtualPath;
if (appUrl != "/")
appUrl = "/" + appUrl;
var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);
return baseUrl;
}
Update for ASP.NET Core / MVC 6:
ASP.NET Core makes this process a bit more painful, especially if you are deep in your code. You have 2 options to get at the HttpContext
1) Pass it in from your controller:
var model = new MyClass(HttpContext);
then in model:
private HttpContext currentContext;
public MyClass(HttpContext currentContext)
{
this.currentContext = currentContext;
}
2) Perhaps the cleaner way is to inject it into your class, which starts with registering the types in your Startup:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddTransient<MyClass, MyClass>();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
then have it injected for you like this:
private HttpContext currentContext;
public MyClass(IHttpContextAccessor httpContextAccessor)
{
currentContext = httpContextAccessor.HttpContext;
}
in either case, here is the updated for .NET Core GetBaseUrl():
public string GetBaseUrl()
{
var request = currentContext.Request;
var host = request.Host.ToUriComponent();
var pathBase = request.PathBase.ToUriComponent();
return $"{request.Scheme}://{host}{pathBase}";
}
In Code:
Url.Content("~/");
MVC3 Razor Syntax:
#Url.Content("~/")
Maybe it is extension or modification of the answers posted here but I use simply the following line and it works:
Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~")
When my path is: http://host/iis_foldername/controller/action
then I receive : http://host/iis_foldername/
The following snippet works nicely for me in MVC4, and doesn't need an HttpContext available:
System.Web.HttpRuntime.AppDomainAppVirtualPath
The trick with relying upon IIS is that IIS bindings can be different from your public URLs (WCF I'm looking at you), especially with multi-homed production machines. I tend to vector toward using configuration to explicitly define the "base" url for external purposes as that tends to be a bit more successful than extracting it from the Request object.
For an absolute base URL use this. Works with both HTTP and HTTPS.
new Uri(Request.Url, Url.Content("~"))
This is a conversion of an asp.net property to MVC . It's a pretty much all singing all dancing get root url method.
Declare a helper class:
namespace MyTestProject.Helpers
{
using System.Web;
public static class PathHelper
{
public static string FullyQualifiedApplicationPath(HttpRequestBase httpRequestBase)
{
string appPath = string.Empty;
if (httpRequestBase != null)
{
//Formatting the fully qualified website url/name
appPath = string.Format("{0}://{1}{2}{3}",
httpRequestBase.Url.Scheme,
httpRequestBase.Url.Host,
httpRequestBase.Url.Port == 80 ? string.Empty : ":" + httpRequestBase.Url.Port,
httpRequestBase.ApplicationPath);
}
if (!appPath.EndsWith("/"))
{
appPath += "/";
}
return appPath;
}
}
}
Usage:
To use from a controller:
PathHelper.FullyQualifiedApplicationPath(ControllerContext.RequestContext.HttpContext.Request)
To use in a view:
#using MyTestProject.Helpers
PathHelper.FullyQualifiedApplicationPath(Request)
In MVC _Layout.cshtml:
<base href="#Request.GetBaseUrl()" />
Thats what we use!
public static class ExtensionMethods
{
public static string GetBaseUrl(this HttpRequestBase request)
{
if (request.Url == (Uri) null)
return string.Empty;
else
return request.Url.Scheme + "://" + request.Url.Authority + VirtualPathUtility.ToAbsolute("~/");
}
}
This works fine for me (also with a load balancer):
#{
var urlHelper = new UrlHelper(Html.ViewContext.RequestContext);
var baseurl = urlHelper.Content(“~”);
}
<script>
var base_url = "#baseurl";
</script>
Especially if you are using non-standard port numbers, using Request.Url.Authority appears like a good lead at first, but fails in a LB environment.
You could have a static method that looks at HttpContext.Current and decides which URL to use (development or live server) depending on the host ID. HttpContext might even offer some easier way to do it, but this is the first option I found and it works fine.
You can use the following script in view:
<script type="text/javascript">
var BASE_URL = '<%= ResolveUrl("~/") %>';
</script>
For ASP.NET MVC 4 it is a bit different:
string url = HttpContext.Request.Url.AbsoluteUri;
This is working in ASP .NET MVC 4
In any controller action you can write:
1stline gets the whole url+Query String.
2nd line remove local path & query ,last '/' symbol.
3rd line add '/' symbol at last position.
Uri url = System.Web.HttpContext.Current.Request.Url;
string UrlLink = url.OriginalString.Replace(url.PathAndQuery,"");
UrlLink = String.Concat(UrlLink,"/" );
in simple html and ASP.NET or ASP.NET MVC if you are using tag:
About us
For url with aplication alias like http://example.com/appAlias/... You can try this:
var req = HttpContext.Current.Request;
string baseUrl = string.Format("{0}://{1}/{2}", req.Url.Scheme, req.Url.Authority, req.ApplicationPath);
On the webpage itself:
<input type="hidden" id="basePath" value="#string.Format("{0}://{1}{2}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Authority,
Url.Content("~"))" />
In the javascript:
function getReportFormGeneratorPath() {
var formPath = $('#reportForm').attr('action');
var newPath = $("#basePath").val() + formPath;
return newPath;
}
This works for my MVC project, hope it helps
This was my solution (using .net core 3.1, in an api controller):
string baseUrl = $"{Request.Scheme}://{Request.Headers.Where(h => h.Key == "Host").First().Value}";
For MVC 4:
String.Format("{0}://{1}{2}", Url.Request.RequestUri.Scheme, Url.Request.RequestUri.Authority, ControllerContext.Configuration.VirtualPathRoot);
I put this in the head of my _Layout.cshtml
<base href="~/" />
Maybe it is a better solution.
#{
var baseUrl = #Request.Host("/");
}
using
Base URL
#{
var baseurl = Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port + Url.Content("~");
}
#baseurl
--output
http://localhost:49626/TEST/
In .net core 3.1 I used this approach:
$"{Request.Scheme}://{Request.Host}{Url.Content("~/")}"
The following worked solidly for me
var request = HttpContext.Request;
var appUrl = System.Web.HttpRuntime.AppDomainAppVirtualPath;
if (appUrl != "/")
appUrl = "/" + appUrl + "/";
var newUrl = string.Format("{0}://{1}{2}{3}/{4}", request.Url.Scheme, request.UrlReferrer.Host, appUrl, "Controller", "Action");
Also you can use this. For the razor pages, it is better to use it than the others.
https://ml-software.ch/posts/getting-the-base-url-for-an-asp-net-core-mvc-web-application-in-your-static-javascript-files
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<base href='#Url.AbsoluteContent("~/")'>
<title>#ViewBag.Title - ASP.NET Core Web Application</title>
<!-- ... -->
</head>
<body>
add this function in static class in project like utility class:
utility.cs content:
public static class Utility
{
public static string GetBaseUrl()
{
var request = HttpContext.Current.Request;
var urlHelper = new UrlHelper(request.RequestContext);
var baseUrl = $"{request.Url.Scheme}://{request.Url.Authority}{urlHelper.Content("~")}";
return baseUrl;
}
}
use this code any where and enjoy it:
var baseUrl = Utility.GetBaseUrl();
Simply in one line get BaseUrl
string baseUrl = new Uri(Request.Url, Url.Content("~")).AbsoluteUri;
//output example: https://stackoverflow.com
Quite simple question. I have the following code
#Html.Raw(following.Description).ToString()
when this comes from database it has some markup in it (its a forum post but i want to show a snippet in the list without the markup
is there any way to remove this and replace this line or shall I just regex it from the controller?
Here is a utility class extension method that is able to strip tags from fragments without using Regex:
public static string StripTags(this string markup)
{
try
{
StringReader sr = new StringReader(markup);
XPathDocument doc;
using (XmlReader xr = XmlReader.Create(sr,
new XmlReaderSettings()
{
ConformanceLevel = ConformanceLevel.Fragment
// for multiple roots
}))
{
doc = new XPathDocument(xr);
}
return doc.CreateNavigator().Value; // .Value is similar to .InnerText of
// XmlDocument or JavaScript's innerText
}
catch
{
return string.Empty;
}
}
Hi I am currently using the asp.net MVC 4 rc with System.Web.Optimization. Since my site needs to be localized according to the user preference I am working with the jquery.globalize plugin.
I would very much want to subclass the ScriptBundle class and determine what files to bundle according to the System.Threading.Thread.CurrentThread.CurrentUICulture. That would look like this:
bundles.Add(new LocalizedScriptBundle("~/bundles/jqueryglobal")
.Include("~/Scripts/jquery.globalize/globalize.js")
.Include("~/Scripts/jquery.globalize/cultures/globalize.culture.{0}.js",
() => new object[] { Thread.CurrentThread.CurrentUICulture })
));
For example if the ui culture is "en-GB" I would like the following files to be picked up (minified of course and if possible cached aswell until a script file or the currentui culture changes).
"~/Scripts/jquery.globalize/globalize.js"
"~/Scripts/jquery.globalize/globalize-en-GB.js" <-- if this file does not exist on the sever file system so fallback to globalize-en.js.
I tried overloading the Include method with something like the following but this wont work because it is not evaluated on request but on startup of the application.
public class LocalizedScriptBundle : ScriptBundle
{
public LocalizedScriptBundle(string virtualPath)
: base(virtualPath) {
}
public Bundle Include(string virtualPathMask, Func<object[]> getargs) {
string virtualPath = string.Format(virtualPathMask, getargs());
this.Include(virtualPath);
return this;
}
}
Thanks
Constantinos
That is correct, bundles should only be configured pre app start. Otherwise in a multi server scenario, if the request for the bundle is routed to a different server other than the one that served the page, the request for the bundle resource would not be found.
Does that make sense? Basically all of your bundles need to be configured and defined in advance, and not dynamically registered on a per request basis.
take a look: https://stackoverflow.com/questions/18509506/search-and-replace-in-javascript-before-bundling
I coded this way for my needs:
public class MultiLanguageBundler : IBundleTransform
{
public void Process(BundleContext context, BundleResponse bundle)
{
var content = new StringBuilder();
var uicult = Thread.CurrentThread.CurrentUICulture.ToString();
var localizedstrings = GetFileFullPath(uicult);
if (!File.Exists(localizedstrings))
{
localizedstrings = GetFileFullPath(string.Empty);
}
using (var fs = new FileStream(localizedstrings, FileMode.Open, FileAccess.Read))
{
var m_streamReader = new StreamReader(fs);
var str = m_streamReader.ReadToEnd();
content.Append(str);
content.AppendLine();
}
foreach (var file in bundle.Files)
{
var f = file.VirtualFile.Name ?? "";
if (!f.Contains("localizedstrings"))
{
using (var reader = new StreamReader(VirtualPathProvider.OpenFile(file.VirtualFile.VirtualPath)))
{
content.Append(reader.ReadToEnd());
content.AppendLine();
}
}
}
bundle.ContentType = "text/javascript";
bundle.Content = content.ToString();
}
private string GetFileFullPath(string uicult)
{
if (uicult.StartsWith("en"))
uicult = string.Empty;
else if (!string.IsNullOrEmpty(uicult))
uicult = ("." + uicult);
return Kit.ToAbsolutePath(string.Format("~/Scripts/locale/localizedstrings{0}.js", uicult));
}
}
I am having trouble with my static content being cached on the client (by static I mean js,css,jpeg,gif, etc). (and by client I mean its my development machine most of the time).
As a result, page is either throwing a script error, or not displaying correctly. I am not a Rails developer but I read a few books about it back in time. One thing I remember well is that it appends some magic version number to the end of the included file, so it becomes
<script src="~/Scripts/Invoice.js?201112091712" type="text/javascript"></script>
and if you modify that content file it generates a new version number, so it generates a different include statement, as a result, client thinks it is a new content, and it loads it without checking its cache.
Does asp.net-mvc 3 & IIS 7 support this, or do you know any tools that mimic this behaviour?
Thanks, Hazım
I have this done already in one of my projects, feel free to use my helpers if you like them :
public static class VersionedContentExtensions
{
public static MvcHtmlString VersionedScript(this HtmlHelper html, string file)
{
return VersionedContent(html, "<script src=\"{0}\" type=\"text/javascript\"></script>", file);
}
public static MvcHtmlString VersionedStyle(this HtmlHelper html, string file)
{
return VersionedContent(html, "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\">", file);
}
private static MvcHtmlString VersionedContent(this HtmlHelper html, string template, string file)
{
string hash = HttpContext.Current.Application["VersionedContentHash_" + file] as string;
if (hash == null)
{
string filename = HttpContext.Current.Server.MapPath(file);
hash = GetMD5HashFromFile(filename);
HttpContext.Current.Application["VersionedContentHash_" + file] = hash;
}
return MvcHtmlString.Create(string.Format(template, file + "?v=" + hash));
}
private static string GetMD5HashFromFile(string fileName)
{
FileStream file = new FileStream(fileName, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
}
use them like this :
#Html.VersionedScript("/Scripts/sccat.core.js")
I did something like this:
<script src="<%= GenerateScriptUrl("~/Scripts/Invoide.js") %>"></script>
In GenerateScriptUrl method, I write the content of the file, calculate the md5 value then get the url with version number. The url would be cached so it will be calculated twice. I also create an handler (not exposed to the user) to clear the cache. So the process doesn't need to be restarted when the file content is changed.
You can also get the version number with the last modified or thing. You can even monitor the file's change by FileSystemWatcher, etc.
Hope it helps.
Try this which adds the file modified time
public static class UrlHelperExtentention
{
public static string VersionedContent(this UrlHelper urlHelper,
string contentPath)
{
string versionedContent= urlHelper.Content(contentPath);
string modified= File.GetLastWriteTime(
HostingEnvironment.MapPath(contentPath))
.ToString("yyyyMMddhhmm");
if (result.Contains('?'))
versionedContent += "&" + modified;
else
versionedContent += "?" + modified;
return versionedContent;
}
}
Then
<script src="#Url.VersionedContent("~/js/Home.js")" type="text/javascript"/>
My code access a file which is in "Conf" directory inside my project directory. I am currently opening the file using absolute path like below:
File.ReadAllLines("C:\project name\Conf\filename");
I was thinikng if it's possible to use the relative path like
File.ReadAllLines("/Conf/filename");
But it's not working; as expected it throws exception. I did checked MSDN (link below) but seems "ReadAllLines()" methods doesn't accept relative path.
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
Any idea, how can I use the relative path instead using absolute path?
Thanks,
Rahul
This is my favorite way of doing it.
Make your file an embedded resource.
/// <summary>
/// This class must be in the same folder as the embedded resource
/// </summary>
public class GetResources
{
private static readonly Type _type = typeof(GetResources);
public static string Get(string fileName)
{
using (var stream =
_type.Assembly.GetManifestResourceStream
(_type.Namespace + "." + fileName))
{
if (stream != null)
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
throw new FileNotFoundException(fileName);
}
}
As stated in MSDN you cannot use a relative path, however you might be able to use either Environment.CurrentDirectory or System.Reflection.Assembly.GetExecutingAssembly().Location
To make things simple, use the following:
string current_path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
string[] lines_from_file = System.IO.File.ReadAllLines(current_path + "/Conf/filename");
...additional black magic here...