简体   繁体   中英

How to get the url of the current view?

Currently in a razor view I'm doing:

var request = Url.ActionContext.HttpContext.Request;
var builder = new UriBuilder(request.Scheme, request.Host.Host, request.Host.Port.Value, request.Path, request.QueryString.Value);

A problem with the above is that the port and query string might not be available.

Is there a better and shorter way to get the view's url?

If you want to get only the full URL, you can use these two Extension methods

@using Microsoft.AspNetCore.Http.Extensions


string fullUrl1 = Request.GetDisplayUrl();
 //or
string fullUrl2 = Request.GetEncodedUrl();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM