How can I get complete and base type of URL in C# asp.net MVC web-application, controller, for example, if my web-application URL is https://qawithexperts.com/questions/111/invalid-uri-the-format-of-the-uri-could-not-be-determined-er
https://qawithexperts.com //base url
https://qawithexperts.com/questions/111/invalid-uri-the-format-of-the-uri-could-not-be-determined-er // complete url
/questions/111/invalid-uri-the-format-of-the-uri-could-not-be-determined-er // after base part of it
how can I get all types of URL in C#?
I would like to save "after base part" of URL in database for some purpose, thanks
You can get the base Url(https://qawithexperts.com) in C# using the code below
//Get request from Current HttpContext
var request = HttpContext.Current.Request;
//request.Url.Scheme gives output as https/http
//while request.Url.Authority give us Domain name
var baseUrl= request.Url.Scheme+"://"+ request.Url.Authority;
Continuing the above code, you can get absolute URL(or you can say after base URL, ie. like /questions/111/invalid-uri-the-format-of-the-uri-could-not-be-determined-er ) using the code below
request.Path; //gives you absolute URL
//or
request.Url.AbsolutePath; // also does the same thing to provide you '/questions/112/how-to-get-complete-url-and-base-url-in-mvc'
Using the above C# asp.net code you can save these URL (absolute & web-base url) in database or do something else with them.
You can create a function like this in C# to get Complete URL, base URL and absolute URL
public string GetBaseUrl()
{
var request = HttpContext.Current.Request;
var appUrl = HttpRuntime.AppDomainAppVirtualPath;
if (appUrl != "/")
appUrl = "/" + appUrl;
var baseUrl = string.Format("{0}://{1}{2}",
request.Url.Scheme, //request.Url.Scheme gives https or http
request.Url.Authority, //request.Url.Authority gives qawithexperts/com
appUrl); //appUrl = /questions/111/ok-this-is-url
return baseUrl; //this will return complete url
}
I found this small code snippet a few months back & it works for me
Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~")
For example, if the path is: https://domain.com/subfolder/controller/action
Then it will return : http://domain.com/subfolder/
For after base path you can use
Request.Url.AbsolutePath //returns /subfolder/controller/action
OR you can use
Request.RawUrl //returns /subfolder/controller/action
Above solutions works well for .NET MVC, but if you are working on .NET Core, you need to use below code
@string.Format("{0}://{1}{2}{3}", Context.Request.Scheme, Context.Request.Host, Context.Request.Path, Context.Request.QueryString)
OR
If you want to get URL in Razor View of .NET Core Application, then
@using Microsoft.AspNetCore.Http.Extensions
<a href="@Context.Request.GetEncodedUrl()">Link to myself</a>
Or from .NET Core 2.0 of above, you can also use GetEncodedPathAndQuery which returns "The path and query off of request
".
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly