Thursday, September 17, 2009 10:41 AM srkirkland

A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

ASP.NET MVC comes with a UrlHelper class in the System.Web.Mvc.Controller.Url namespace, which you can access through the Url property of any controller.  This provides some handy methods to get the url of an action or route, among other things.  For example, Url.Action(“About”, “Home”) will return the string “/Home/About”, which is the relative url of the About action on the Home controller.

I recently had a need to generate an absolute Url (to put in an email), so I wrote a quick extension method to the UrlHelper class that I thought might be useful to someone else.  The code is as follows:

   1: public static class UrlExtensions
   2: {
   3:     public static string AbsoluteAction(this UrlHelper url, string action, string controller, object routeValues)
   4:     {
   5:         Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
   6:  
   7:         string absoluteAction = string.Format("{0}{1}",
   8:                                               requestUrl.GetLeftPart(UriPartial.Authority),
   9:                                               url.Action(action, controller, routeValues));
  10:  
  11:         return absoluteAction;
  12:     }
  13: }

Basically is takes the same values as Url.Action (or at least one of the Url.Action signatures) and adds the “left part” of the authority (see the MSDN Library GetLeftPart Docs).

So if you write the following code:

   1: Url.AbsoluteAction("Edit", "Users", new {id="username"});

You should get something like the following result if you are running it locally:

http://localhost:50717/Users/Edit/username

Enjoy!

Filed under: , , , ,

Comments

# Twitter Trackbacks for A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC - Scott's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC - Scott's Blog         [asp.net]        on Topsy.com

# ASP.NET MVC Archived Blog Posts, Page 1

Monday, September 21, 2009 1:00 AM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Friday, October 16, 2009 3:45 PM by Robert

FYI:  Your project will need to reference System.Web.Abstractions and System.Web.Routing to get this to work.

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Wednesday, April 28, 2010 12:39 AM by Norman H Harebottle

Just exactly what I needed for implementing RPXNow.com authentication handling on my site.  Thanks!

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Wednesday, December 14, 2011 7:11 PM by DierE

buy <a href=vobtoaviconverter.blinkweb.com/>vob to avi converter</a>  , just clicks away   online

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Monday, November 19, 2012 6:08 AM by wgrbclse@gmail.com

Romances remain in the event that each one comrade says he has a small favorable position during the extra.

[url=www.casquebeatsbydrefr2013.com]beats by dre[/url]

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Monday, December 24, 2012 9:47 PM by BorErurbtum

<a href=www.restorebeautynow.com/.../a>  products for skin care. You can buy , eye lift cream, etc. You can also ingredients, or you buy the products directly from the store you will and olive oil. Here are the basics for the transition to natural skin sun exposure until the redness goes away to prevent further skin damage. essential but too much of it can be a risk. Three-Step Method Using

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Wednesday, January 09, 2013 4:26 AM by mjmrgnsqqfr@gmail.com

Those most inopportune way to neglect another person is to be staying correctly adjacent to these recognizing you are憑t|capital t|big t|to|testosterone levels|testosterone|w not|longer|l|r|g|s|h|d|p|T|metric ton|MT|tonne} make them.

destockchine www.destockchinefr.fr/.../nike-shox-nz-2-pas-cher

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Monday, January 28, 2013 9:33 AM by brandgxjywwbk@gmail.com

大島がリーダーになってからAKBはそういうグループの色眼鏡で見られてる

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Tuesday, February 26, 2013 8:45 AM by Mandymxg

<a href="www.michaelkorsoutletazr.com/" title="Michael Kors outlet">Michael Kors outlet</a></strong> ghoakudp

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Friday, March 22, 2013 10:09 PM by vqfhwicaxcw@gmail.com

During wealth the great friends understand states; at difficulty we realize the great friends. bonnet casquette http://e55.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Friday, April 05, 2013 5:55 AM by vzszdpepf@gmail.com

It's possible Fin requires me to fulfill two or three entirely wrong visitors before getting to getting together with a good choice, so as that when i conclusively meet the man or women, you will learn how to always be happier. veste femme http://www.salansa.com/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Saturday, April 06, 2013 7:36 PM by bumcehrwjx@gmail.com

Will not talk about all of your peace to one a lot less privileged versus one self. jef chaussures http://ruemee.com/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Saturday, April 06, 2013 8:40 PM by ddsscbv@gmail.com

Although person doesn‘d love you how you will would like them to assist you to,doesn‘d require they begin to get into‘d love you operating they want. tati http://ruenee.com/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Thursday, April 11, 2013 1:41 AM by iztavlugvc@gmail.com

Not ever look down upon, despite the fact that you happen to be sorrowful, since you also never know who it is being knocked crazy about your new smile. groupon france http://grouponfr.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Thursday, April 11, 2013 3:39 AM by lwdmwtuveyq@gmail.com

A friendly relationship is most likely the Coptis groenlandica which unfortunately connections all the hearts and minds of the environment. ruezee.com http://ruezee.com/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Thursday, April 11, 2013 7:10 AM by pedqjcj@gmail.com

Genuine association foresees the requirements of many other and not exclaim you'll find it individual. ckgucci http://ckgucci.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Thursday, April 11, 2013 7:33 AM by dobtcbpjtt@gmail.com

Of prosperity the best neighbors appreciate everyone; found in difficulty small children the best neighbors. sarenzalando http://sarenza-lando.com/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Friday, April 12, 2013 7:45 AM by hybnysvtu@gmail.com

adore Buy Burberry Bags - a little bit diverse than e ones the others have - diverse is fantastic!!

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Saturday, April 13, 2013 5:28 AM by bkzwppec@gmail.com

Look fantastic, actually feel wonderful along with your child is in love with Buy Burberry!

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Saturday, April 13, 2013 4:01 PM by kutxnbyyuu@gmail.com

Relationship would be the goldthread which usually jewelry all the minds of all world. tnrequintn2013.com http://www.tnrequintn2013.com/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Sunday, April 14, 2013 6:26 PM by bkqgyvkbbd@gmail.com

It could be that Graven image hopes for people to meet a selection of false women and men earlier than assembly a good choice, in order that muscle building lastly satisfy the people, we will recognize how to get thankful. sarenza soldes http://i88.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Monday, April 15, 2013 1:30 PM by nipeczrl@gmail.com

I adore a Miu Miu Outlet Concerning Five sets !!! I just get them how to university regular they're fabbb!!!!

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Monday, April 15, 2013 5:02 PM by okrptypgstf@gmail.com

What if Divinity wants states based on a couple incorrectly citizens in advance of when gathering the best one, in order for muscle building at last satisfy the guy, we could know how to you should be happier. g88 http://g88.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Tuesday, April 16, 2013 12:55 PM by qxmiuwc@gmail.com

Simply put i totally can try this approach grew to be undoubtedly one of Oprah's much-loved details!!! Now i am basically no Oprah these glimmer retro classics really are really undoubtedly one of my personal Issues!!!! I am a large number of enhances after i carry wow power leveling!!!!

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Wednesday, April 17, 2013 6:16 AM by zcwihzyxvc@gmail.com

Get into‘capital t waste material it slow with a fella/great lady,individuals who isn‘capital t ready to waste material the energy giving you. casquette snapback http://www.a88.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Wednesday, April 17, 2013 8:26 AM by rohoitqi@gmail.com

One particular good friend might not be a friend, however a friend are invariably a very good friend. casquette YMCMB http://www.a44.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Wednesday, April 17, 2013 3:20 PM by emblnpvpxt@gmail.com

Get into‘h waste material your for the man/females,exactly who isn‘h prepared waste material an individual's instance upon you. fr marque http://frmarquefr.com/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Wednesday, April 17, 2013 7:01 PM by tyomxi@gmail.com

Like may possibly be the exclusive satisfied as well as solid solution most typically associated with individual daily existence. b66.fr http://www.b66.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Thursday, April 18, 2013 3:07 AM by ckeujdnbxty@gmail.com

This is actually items Fifth Celine Bags decide to purchase and so i absolutely adore these the one thing with regards to these kinds of simply because need to water-proof these businesses stronger now regardless if it is usually blizzarding up you could continue to have quite a few glow inside of your attire and not have to keep worrying about messing up your current glow Celine Bags! these kinds of Celine Bags complement anything1 may all of the charcoal an individual's and provide them everything from items work uniform, pants,so that you furthermore shorts and then dresses!

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Thursday, April 18, 2013 8:43 AM by lxabslhz@gmail.com

My family and i convey great Michael Kors Handbags by way of a lot of stuff, especially strecth straight-legs, I really like these kind of Michael Kors Handbags.

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Saturday, April 20, 2013 2:43 PM by cyvifhfeqw@gmail.com

To the world you may well be yourself, however to at least one customer you may well be all mankind. tn pas cher http://www.5fr.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Saturday, April 20, 2013 4:44 PM by wujpyb@gmail.com

Adoration stands out as the one happy and therefore suitable solution associated with a person's lifespan. casquette ny http://www.b22.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Saturday, April 20, 2013 7:48 PM by lcfdufzhb@gmail.com

Could be Who hopes our business in order to satisfy various mistaken men and women earlier than matching the most appropriate one, make sure if we at long last meet the one, in this article know how to get gracious. casquette la http://www.b33.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Sunday, April 21, 2013 3:42 AM by fwucidgsqr@gmail.com

Take pleasure in is actually fallible in the begin, nevertheless it springs up more forceful as we age whether it's completely provided with. lunettes prada http://www.g77.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Monday, April 22, 2013 3:30 AM by tusbsn@gmail.com

An authentic companion is just one who else overlooks the disappointments and in addition tolerates the successes. frmarquefr.fr http://frmarquefr.fr/

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Monday, April 22, 2013 3:49 PM by buirnke@gmail.com

We are a group of volunteers and opening a new scheme in our community. Your site offered us with valuable info to work on. You've done a formidable job and our whole community will be thankful to you.

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Tuesday, April 23, 2013 9:21 AM by uyoymymjic@gmail.com

Product had been great, and also good interaction overly because of customer satisfaction! Enjoy my best christian louboutin sale concise memorable on proverb shade!

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Saturday, May 04, 2013 11:51 AM by mqlrevvupic@gmail.com

The hermes wallets truly feel wonderful in all cases. Many thanks for this sort of an amazing products!

# re: A UrlHelper Extension For Creating Absolute Action Paths in ASP.NET MVC

Wednesday, May 08, 2013 9:19 PM by varlfuvv@gmail.com

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you. nike blazer pas cher nike-blazer-vintage-nike-blazer.webnode.fr

Leave a Comment

(required) 
(required) 
(optional)
(required)