A problem with using string replace is that if your absolute path is "/" (site root /Home/Index), the URI gets mangled,
i.e.  http:www.example.com/SomeController/SomeAction

I'd be tempted to create an extension method on UrlHelper to hide some of the ugliness:

    public static class UrlHelperExtensions
    {
public static string AbsoluteAction(this UrlHelper helper, string actionName, string controllerName)
        {
string absUri = helper.RequestContext.HttpContext.Request.Url.AbsoluteUri; string urlPath = helper.RequestContext.HttpContext.Request.Url.PathAndQuery; return absUri.Substring(0, absUri.Length - urlPath.Length) + helper.Action(actionName, controllerName);
        }
    }

then in your controller:

string returnURI = Url.AbsoluteAction("SomeAction","SomeController");



On 12/04/2013 18:51, Greg Low (GregLow.com) wrote:

Hi Nathan/Dave,

This seems to work but seems ugly:

stringreturnURI = HttpContext.Request.Url.AbsoluteUri.Replace(HttpContext.Request.Url.AbsolutePath,"")

                        + Url.Action("SomeAction", "SomeController");

I need to generate it based on where the site is deployed but it needs to be a full URL as it’s passed to a callback function on another site.

Regards,

Greg

Dr Greg Low

1300SQLSQL (1300 775 775) office | +61 419201410 mobile│ +61 3 8676 4913 fax

SQL Down Under| Web: www.sqldownunder.com <http://www.sqldownunder.com/>

*From:*[email protected] <mailto:[email protected]> [mailto:[email protected]] *On Behalf Of *Dave Walker
*Sent:* Friday, 12 April 2013 5:43 PM
*To:* ozDotNet
*Subject:* Re: MVC4 URLs

We use a helper extension method to wrap as have configurable cdn esp. for images and other static resources. Was the only way we found we could do it. Extra complexity for us is we wrap a SquishIt bundle up as well to turn on/off minification and combination of files.


On 12 Apr 2013, at 08:32, Nathan Schultz <[email protected] <mailto:[email protected]>> wrote:

    Instead of using Url.Action, couldn't you just write your own <a
    href>?

    On 12 April 2013 11:17, Greg Low (GregLow.com
    <http://GregLow.com>) <[email protected] <mailto:[email protected]>>
    wrote:

        Hi Folks,

        In MVC4, in the code for a controller, what’s the best way to
        calculate the fully qualified URL for a particular action?

        Eg: If I use

        Url.Action(“SomeAction”,”SomeController”)

        The intellisense for Action says “gets a fully qualified URL”.
        However what I get back is:

        /SomeController/SomeAction

        What I want is:

        http://www.whateversiteIhit.com/SomeController/SomeAction
        <http://www.somesite.com/SomeController/SomeAction>

        As I need to pass it to an external callback.

        What’s the best way to do that?

        Regards,

        Greg

        Dr Greg Low

        CEO and Principal Mentor

        *SQL Down Under*

        SQL Server MVP and Microsoft Regional Director

        1300SQLSQL (1300 775 775) office | +61 419201410
        <tel:%2B61%20419201410>mobile│ +61 3 8676 4913
        <tel:%2B61%203%208676%204913>fax

        Web: www.sqldownunder.com <http://www.sqldownunder.com/>

        **


Reply via email to