Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Url.RouteUrl can be simpler for common patterns. #1966

Closed
shanselman opened this issue Feb 5, 2015 · 10 comments
Closed

Url.RouteUrl can be simpler for common patterns. #1966

shanselman opened this issue Feb 5, 2015 · 10 comments

Comments

@shanselman
Copy link

This is a common pattern:

dinner = await _repository.CreateDinnerAsync(dinner);
var url = Url.RouteUrl("GetDinnerById", new { id = dinner.DinnerId }, Request.Scheme, Request.Host.ToUriComponent());

Context.Response.StatusCode = (int)HttpStatusCode.Created;
Context.Response.Headers["Location"] = url;
return new JsonResult(dinner);

I find

var url = Url.RouteUrl("GetDinnerById", new { id = dinner.DinnerId }, Request.Scheme, Request.Host.ToUriComponent())

mixing two things and a little tedious. The last part

Request.Scheme, Request.Host.ToUriComponent())

is kind of administrivia. Can we make it more like?

var Url.RouteUrlFull("GetDinnerById", new { id = dinner.DinnerId });

or

var Url.RouteUrlAbsolute("GetDinnerById", new { id = dinner.DinnerId });
@davidfowl
Copy link
Member

What about if that method returned a builder kinda like UriBuilder that you could then use to get the full url?

var urlBuilder = Url.RouteUrlAsBuilder("GetDinnerById", new { id = dinner.DinnerId });
var full = urlBuilder.ToFull();

@shanselman
Copy link
Author

So like this?

 var myString = Url.RouteUrlAsBuilder("GetDinnerById", new { id = dinner.DinnerId }).ToFull();

Or

 var myString = Url.RouteUrlAsBuilder("GetDinnerById", new { id = dinner.DinnerId }).ToAbsolute();

Still seems like a lot of moving parts to do something I'm gonna do ALL DAY LONG.

@rustd
Copy link

rustd commented Feb 5, 2015

var myString = Url.RouteUrlAsBuilder("GetDinnerById", new { id = dinner.DinnerId }).ToFull(); is much better

@shanselman
Copy link
Author

@rustd But why do I need to RouteUrlAsBuilder? What's the Builder? What's wrong with RouteUrlAsAbsolute and don't even bother with the ToFull. The ToString is implied.

@csharpfritz
Copy link
Member

This does feel a bit tedious... I'd almost want the scheme and host to default to the values the current request came in on. Most of the time, I think these values would stay the same from one request to the next

@yishaigalatzer
Copy link
Contributor

Add an extension method in your code to remove repetitive tediousness?

@yishaigalatzer
Copy link
Contributor

WebApi had this feature named link - We should bring it back:

From WebApi code

    public class UrlHelper
    {
        // lots of other methods here
        // ......
        //

        public virtual string Link(string routeName, object routeValues)
        {
            return Link(routeName, new HttpRouteValueDictionary(routeValues));
        }

        /// <summary>
        /// Creates an absolute URL using the specified route and route data.
        /// </summary>
        /// <param name="routeName">The name of the route to use for generating the URL.</param>
        /// <param name="routeValues">The route data to use for generating the URL.</param>
        /// <returns>The generated URL.</returns>
        [SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "It is safe to pass string here")]
        public virtual string Link(string routeName, IDictionary<string, object> routeValues)
        {
            string link = Route(routeName, routeValues);
            if (!String.IsNullOrEmpty(link))
            {
                link = new Uri(Request.RequestUri, link).AbsoluteUri;
            }

            return link;
        }
   }

@yishaigalatzer yishaigalatzer added this to the 6.0.0-rc1 milestone Feb 6, 2015
@shanselman
Copy link
Author

Fantastic.

On Fri, Feb 6, 2015 at 9:42 AM, Yishai Galatzer notifications@github.com
wrote:

WebApi had this feature named link - We should bring it back:

From WebApi code

public class UrlHelper
{
    // lots of other methods here
    // ......
    //

    public virtual string Link(string routeName, object routeValues)
    {
        return Link(routeName, new HttpRouteValueDictionary(routeValues));
    }

    /// <summary>
    /// Creates an absolute URL using the specified route and route data.
    /// </summary>
    /// <param name="routeName">The name of the route to use for generating the URL.</param>
    /// <param name="routeValues">The route data to use for generating the URL.</param>
    /// <returns>The generated URL.</returns>
    [SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "It is safe to pass string here")]
    public virtual string Link(string routeName, IDictionary<string, object> routeValues)
    {
        string link = Route(routeName, routeValues);
        if (!String.IsNullOrEmpty(link))
        {
            link = new Uri(Request.RequestUri, link).AbsoluteUri;
        }

        return link;
    }

}


Reply to this email directly or view it on GitHub
#1966 (comment).

Scott Hanselman
Donate to fight diabetes: http://hnsl.mn/fightdiabetes

@ajaybhargavb
Copy link
Contributor

3d30fd6

@RehanSaeed
Copy link

See #2810 for AbsoluteRouteUrl, AbsoluteRouteAction and AbsoluteContent methods added to UrlHelper.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants