Skip to content

Instantly share code, notes, and snippets.

@dhlavaty
Last active August 30, 2021 07:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dhlavaty/5636215 to your computer and use it in GitHub Desktop.
Save dhlavaty/5636215 to your computer and use it in GitHub Desktop.
How to get an Absolute URLs in ASP.NET MVC without HttpContext and/or UrlHelper
public static class AbsoluteUrl
{
private static bool initializedAlready = false;
private static Object initlock = new Object();
// AbsoluteUrl without parameters
public static string MVC_Home_Index;
public static string MVC_MyArea_Settings_Index;
// AbsoluteUrl with parameters
public static string MVC_MyArea_Settings_Edit_replacable;
public static string MVC_MyArea_Settings_Edit(Guid? id)
{
// "You must define a specific route for JavaScriptReplacableUrl to work", but it doesn't work for me. So I use this:
return MVC_MyArea_Settings_Edit_replacable + "?" + MVC.MyArea.Settings.EditParams.id + "=" + id.ToString();
}
/// <summary>
/// Initialize only on the first request
/// </summary>
/// <param name="context"></param>
public static void Initialize(System.Web.HttpContext context)
{
if (initializedAlready)
{
return;
}
lock (initlock)
{
if (initializedAlready)
{
return;
}
var url = new UrlHelper(context.Request.RequestContext);
var leftPart = context.Request.Url.GetLeftPart(UriPartial.Authority);
// AbsoluteUrl without parameters
MVC_Home_Index = url.ActionAbsolute(MVC.Home.Index());
MVC_MyArea_Settings_Index = url.ActionAbsolute(MVC.MyArea.Settings.Index());
// AbsoluteUrl with parameters
MVC_MyArea_Settings_Edit_replacable = leftPart + url.JavaScriptReplacableUrl(MVC.MyArea.Settings.Edit());
initializedAlready = true;
}
}
}
/*
Now you don't need System.Web.HttpContext.Current nor System.Web.Mvc.UrlHelper to get Absolute URL in your application
*/
public class Example
{
void TestIt()
{
Task<string> t = Task<string>.Run(() =>
{
// Now you can access absolute URL also in this thread without HttpContext
// returns: http://www.mydomain.com/home/
return AbsoluteUrl.MVC_Home_Index;
// returns: http://www.mydomain.com/my/settings/
return AbsoluteUrl.MVC_MyArea_Settings_Index;
// returns: http://www.mydomain.com/my/settings/edit?id=00000000-0000-0000-0000-000000000000
return AbsoluteUrl.MVC_MyArea_Settings_Edit(Guid.Empty);
});
}
}
public class MvcApplication : System.Web.HttpApplication
{
void Application_BeginRequest(object sender, EventArgs e)
{
// Attempt to perform first request initialization
AbsoluteUrl.Initialize(this.Context);
}
}

How to get an Absolute URLs in ASP.NET MVC without HttpContext and/or UrlHelper

Using T4MVC (https://t4mvc.codeplex.com/) you must have HttpContext to resolve an URL in your application. So it won't work in a different thread for example. So I found this solution. I'm editing this by hand now, but I propose it as a new feature to integrate directly into T4MVC (https://t4mvc.codeplex.com/discussions/444764) .

One can now get an absolute URL of actions in a different thread and don't need an HttpContext nor UrlHelper .

I'm using this with FluentScheduler ( https://github.com/jgeurts/FluentScheduler ) for some background tasks like sending emails with RazorEngine and so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment