How do I get the full url of the page I am on in C

2020-01-24 10:29发布

I need to be able to get at the full URL of the page I am on from a user control. Is it just a matter of concatenating a bunch of Request variables together? If so which ones? Or is there a more simpiler way?

10条回答
小情绪 Triste *
2楼-- · 2020-01-24 10:43

if you need the full URL as everything from the http to the querystring you will need to concatenate the following variables

Request.ServerVariables("HTTPS") // to check if it's HTTP or HTTPS
Request.ServerVariables("SERVER_NAME") 
Request.ServerVariables("SCRIPT_NAME") 
Request.ServerVariables("QUERY_STRING")
查看更多
Melony?
3楼-- · 2020-01-24 10:44

For ASP.NET Core you'll need to spell it out:

@($"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}{Context.Request.QueryString}")

Or you can add a using statement to your view:

@using Microsoft.AspNetCore.Http.Extensions

then

@Context.Request.GetDisplayUrl()

The _ViewImports.cshtml might be a better place for that @using

查看更多
Rolldiameter
4楼-- · 2020-01-24 10:45

Request.RawUrl

查看更多
beautiful°
5楼-- · 2020-01-24 10:50
Request.Url.AbsoluteUri

This property does everything you need, all in one succinct call.

查看更多
唯我独甜
6楼-- · 2020-01-24 10:55

I usually use Request.Url.ToString() to get the full url (including querystring), no concatenation required.

查看更多
Melony?
7楼-- · 2020-01-24 10:58

Better to use Request.Url.OriginalString than Request.Url.ToString() (according to MSDN)

查看更多
登录 后发表回答