.net - Getting absolute URLs using ASP.NET Core MVC 6 -
in mvc 5, had following extension methods generate absolute urls, instead of relative ones:
public static class urlhelperextensions { public static string absoluteaction( urlhelper url, string actionname, string controllername, object routevalues = null) { string scheme = url.requestcontext.httpcontext.request.url.scheme; return url.action(actionname, controllername, routevalues, scheme); } public static string absolutecontent( urlhelper url, string contentpath) { return new uri(url.requestcontext.httpcontext.request.url, url.content(contentpath)).tostring(); } public static string absoluterouteurl( urlhelper url, string routename, object routevalues = null) { string scheme = url.requestcontext.httpcontext.request.url.scheme; return url.routeurl(routename, routevalues, scheme); } }
what equivalent in asp.net core mvc 6?
urlhelper.requestcontext
no longer exists.- you can't hold of
httpcontext
there no longer statichttpcontext.current
property.
as far can see, require httpcontext
or httprequest
objects passed in also. right? there way hold of current request?
am on right track, should domain environment variable, simple appended relative url? better approach?
after rc2 , 1.0 no longer need inject ihttpcontextaccessor
extension class. available in iurlhelper
through urlhelper.actioncontext.httpcontext.request
. create extension class following same idea, simpler since there no injection involved.
public static string absoluteaction( iurlhelper url, string actionname, string controllername, object routevalues = null) { string scheme = url.actioncontext.httpcontext.request.scheme; return url.action(actionname, controllername, routevalues, scheme); }
leaving details on how build injecting accesor in case useful someone. might interested in absolute url of current request, in case take @ end of answer.
you modify extension class use ihttpcontextaccessor
interface httpcontext
. once have context, can httprequest
instance httpcontext.request
, use properties scheme
, host
, protocol
etc in:
string scheme = httpcontextaccessor.httpcontext.request.scheme;
for example, require class configured httpcontextaccessor:
public static class urlhelperextensions { private static ihttpcontextaccessor httpcontextaccessor; public static void configure(ihttpcontextaccessor httpcontextaccessor) { httpcontextaccessor = httpcontextaccessor; } public static string absoluteaction( iurlhelper url, string actionname, string controllername, object routevalues = null) { string scheme = httpcontextaccessor.httpcontext.request.scheme; return url.action(actionname, controllername, routevalues, scheme); } .... }
which can on startup
class (startup.cs file):
public void configure(iapplicationbuilder app) { ... var httpcontextaccessor = app.applicationservices.getrequiredservice<ihttpcontextaccessor>(); urlhelperextensions.configure(httpcontextaccessor); ... }
you come different ways of getting ihttpcontextaccessor
in extension class, if want keep methods extension methods in end need inject ihttpcontextaccessor
static class. (otherwise need ihttpcontext
argument on each call)
just getting absoluteuri of current request
if want absolute uri of current request, can use extension methods getdisplayurl
or getencodedurl
urihelper
class. (which different urlhelper)
getdisplayurl. returns combined components of request url in un-escaped form (except querystring) suitable display. format should not used in http headers or other http operations.
getencodedurl. returns combined components of request url in escaped form suitable use in http headers , other http operations.
in order use them:
- include namespace
microsoft.aspnet.http.extensions
. - get
httpcontext
instance. available in classes (like razor views), in others might need injectihttpcontextaccessor
explained above. - then use them in
this.context.request.getdisplayurl()
an alternative methods manually crafting absolute uri using values in httpcontext.request
object (similar requirehttpsattribute does):
var absoluteuri = string.concat( request.scheme, "://", request.host.touricomponent(), request.pathbase.touricomponent(), request.path.touricomponent(), request.querystring.touricomponent());
Comments
Post a Comment