Find URL scheme - c#

I want to check whether the incoming url having http or https in c#? Is there any default method to find the url scheme(http/https)?

You could use Uri.Scheme to check and see if it is https.
You could also just use Request.IsSecureConnection

var currentUrl = System.Web.HttpContext.Current.Request.Url;
if (!currentUrl.Scheme.Equals(Uri.UriSchemeHttps,
StringComparison.CurrentCultureIgnoreCase))
For more information : Uri.UriSchemeHttps Field

Related

How to remove segment URL when use Response.Redirect

Current page URL is: localhost/Controller1/Index#select_state
When I call Response.Redirect("/"),
new URL is: localhost#select_state
How to remove #select_state in new ULR?
Thank you!
Before you are doing Redirect operation you have to check whether the database provided url has the valid or not. After you have to use the response. Redirect operation. Or try with below code, to achieve your requirement.
dynamicurl = "/";
url = "localhost//Controller1/Index"
Response.Redirect(url + dynamicurl);
You could try redirecting to an absolute URI:
Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Host);

HTTPS availablity of http url

I am having list of http urls, I need to find https url is available or not. Example : http://www.apra.gov.au/Insight/Pages/insight-issue2-2017.html, need to check whether https is available on the same domain via c# code. since I have a list of 5k http urls. I need to verify all these url available on HTTPS?
You can probably do a simle string replace (http: = https: ) and then loop through them all calling httpget to check:
Psuedo code:
var httpClient = new HttpClient()
foreach(var url in urls)
var httpUrl = url.Replace("http:","https:");
httpClient.Get(url);

C# Uri class - stripping off a port number from Authority

I'm trying tu use System.Uri to strip off various information.
E.g. it gets me Uri.Authority.
When my URI is http://some.domain:52146/something, Uri.Authority.ToString() gives me "some.domain:52146".
I'd rather have "some.domain" and the port with a separate call.
Any ideas whow I could strip off the :port_number stuff most elegantly, either with a Uri-method I don't know of or with some string manipulation?
And getting back the http:// would also be useful (to know for example whether it's http or https).
Use Uri.Host and Uri.Port:
Uri uri = new Uri("http://some.domain:52146/something");
string host = uri.Host; // some.domain
int port = uri.Port; // 52146
Since I learnt about the properties Uri.Host and Uri.Port now I know that Uri.Scheme gives me the protocol.

ASP.NET: parse url having # (hash) sign

I need to parse url that has something after # (hash) sign in my asp.net application. How to do it easily?
Thank you,
You're looking for the Uri class:
new Uri(someString).Fragment
Note that the hash is not sent to the server in an HTTP request.
url.Substring(url.IndexOf('#') + 1)
...where "url" is a string containing the url in question
This is called "hash sign" URI.
After client gets PAGE responsed including js,
the contents after '#' would be handled by client using responsed js to get "real" URL for redirection.
SEE: https://www.w3.org/2001/tag/2011/01/HashInURI-20110115#References
Omg, I meant fragment (after #) part on the server... Though I've looked thrugh and found that it seems to be impossible......

Getting the HTTP Referrer in ASP.NET

I'm looking for a quick, easy and reliable way of getting the browser's HTTP Referrer in ASP.Net (C#). I know the HTTP Referrer itself is unreliable, but I do want a reliable way of getting the referrer if it is present.
You could use the UrlReferrer property of the current request:
Request.UrlReferrer
This will read the Referer HTTP header from the request which may or may not be supplied by the client (user agent).
Request.Headers["Referer"]
Explanation
The Request.UrlReferrer property will throw a System.UriFormatException if the referer HTTP header is malformed (which can happen since it is not usually under your control).
Therefore, the Request.UrlReferrer property is not 100% reliable - it may contain data that cannot be parsed into a Uri class. To ensure the value is always readable, use Request.Headers["Referer"] instead.
As for using Request.ServerVariables as others here have suggested, per MSDN:
Request.ServerVariables Collection
The ServerVariables collection retrieves the values of predetermined environment variables and request header information.
Request.Headers Property
Gets a collection of HTTP headers.
Request.Headers is a better choice than Request.ServerVariables, since Request.ServerVariables contains all of the environment variables as well as the headers, where Request.Headers is a much shorter list that only contains the headers.
So the most reliable solution is to use the Request.Headers collection to read the value directly. Do heed Microsoft's warnings about HTML encoding the value if you are going to display it on a form, though.
Use the Request.UrlReferrer property.
Underneath the scenes it is just checking the ServerVariables("HTTP_REFERER") property.
Like this: HttpRequest.UrlReferrer Property
Uri myReferrer = Request.UrlReferrer;
string actual = myReferrer.ToString();
I'm using .Net Core 2 mvc,
this one work for me ( to get the previews page) :
HttpContext.Request.Headers["Referer"];
Since Google takes you to this post when searching for C# Web API Referrer here's the deal: Web API uses a different type of Request from normal MVC Request called HttpRequestMessage which does not include UrlReferrer. Since a normal Web API request does not include this information, if you really need it, you must have your clients go out of their way to include it. Although you could make this be part of your API Object, a better way is to use Headers.
First, you can extend HttpRequestMessage to provide a UrlReferrer() method:
public static string UrlReferrer(this HttpRequestMessage request)
{
return request.Headers.Referrer == null ? "unknown" : request.Headers.Referrer.AbsoluteUri;
}
Then your clients need to set the Referrer Header to their API Request:
// Microsoft.AspNet.WebApi.Client
client.DefaultRequestHeaders.Referrer = new Uri(url);
And now the Web API Request includes the referrer data which you can access like this from your Web API:
Request.UrlReferrer();
string referrer = HttpContext.Current.Request.UrlReferrer.ToString();
Sometime you must to give all the link like this
System.Web.HttpContext.Current.Request.UrlReferrer.ToString();
(in option when "Current" not founded)
Using .NET Core or .NET 5 I would recommend this:
httpContext.Request.Headers.TryGetValue("Referer", out var refererHeader)
Belonging to other reply, I have added condition clause for getting null.
string ComingUrl = "";
if (Request.UrlReferrer != null)
{
ComingUrl = System.Web.HttpContext.Current.Request.UrlReferrer.ToString();
}
else
{
ComingUrl = "Direct"; // Your code
}

Categories