Skip to content

Instantly share code, notes, and snippets.

@co89757
Created January 26, 2019 13:09
Show Gist options
  • Save co89757/c6d134e78edf333df3d2657db2304fad to your computer and use it in GitHub Desktop.
Save co89757/c6d134e78edf333df3d2657db2304fad to your computer and use it in GitHub Desktop.
generate url link from route template and routedictionaryvalues in .net core
using Microsoft.AspNetCore.Routing.Template;
using Microsoft.AspNetCore.Routing;
using System.Text.Encodings.Web;
using Microsoft.Extensions.ObjectPool;
using Microsoft.AspNetCore.Routing.Internal;
namespace chat
{
public class UrlBuilder
{
public static string BuildUri(string routeTemplate, RouteValueDictionary values ){
var template = TemplateParser.Parse(routeTemplate);
var urlencoder = UrlEncoder.Create(new TextEncoderSettings{});
var poolpolicy = new UriBuilderContextPooledObjectPolicy();
var pool = new DefaultObjectPool<UriBuildingContext>(poolpolicy);
var binder = new TemplateBinder(urlEncoder: urlencoder, pool: pool, template: template, defaults: null);
var x = binder.BindValues(values);
return x;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment