2

I am currently migrating MVC Project to Asp.Net Core Project where in Layout page i used one piece of code to get the current base url.

e.g: If i run MVC application hosted in IIS server then i used Virtual directory to have the MVC application like below:

https://mydomain/myMVCApplication/Login

Then using below code in view i can get the Url till Virtual directory

https://mydomain/myMVCApplication/

 var baseUrl = @Html.Raw(System.Web.HttpUtility.JavaScriptStringEncode(new Uri(new Uri(this.Context.Request.Url.GetLeftPart(UriPartial.Authority)), Url.Content("~/")).ToString(), true));

But when migrate in asp.net core view then Here Context.Request.Url where Url is not a property of Context.Request.

Please suggest here to fix above issue and get the same result.

3 Answers 3

5

In Asp.net Core application, you could try to get the URL using the Microsoft.AspNetCore.Http.HttpRequest class or the Microsoft.AspNetCore.Http.Extensions. Then, using ViewData to store the URL in the action method, and display the url on the View Page.

  1. Use the Microsoft.AspNetCore.Http.HttpRequest class

         ViewData["Message"] = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";
    

    or

         ViewData["Message"] = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.Path}";
    
  2. Use the Microsoft.AspNetCore.Http.Extensions

    Using the following code:

         var displayUrl = UriHelper.GetDisplayUrl(Request);
         var urlBuilder =
         new UriBuilder(displayUrl)
         {
             Query = null,
             Fragment = null
         };
         string url = urlBuilder.ToString();
         ViewData["Message"] = url;
    

Code in the View page:

@{ 
    if (ViewData["Message"] != null)
    {
        @ViewData["Message"]
    }; 
} 
Sign up to request clarification or add additional context in comments.

1 Comment

this.Request.PathBase did the job using this i am getting the domain with Virtual directory.
1

You can try this Microsoft.AspNetCore.Http.Extensions.UriHelper.GetFullUrl(Request)

2 Comments

What is Request here? please explain.
Getting compile error: UriHelper does not contain a definition of GetFullUrl.
0

You can use this

@{
var baseUrl = HttpContext.Request.IsHttps ? $"https://{HttpContext.Request.Host}" : $"http:{HttpContext.Request.Host}"
}

5 Comments

curly braces inside double quotes is not providing the correct format. kindly modify your code and provide.
I am using string interpolation with $, curly braces should work, can you paste what error are you facing?
When i used the suggested code then i found like there was compilation error in HttpContext class. I added refrence in top of view inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor then used like this: var baseUrl = HttpContextAccessor.HttpContext.Request.IsHttps ? $"https://{HttpContext.Request.Host}" : $"http:{HttpContext.Request.Host}" and getting console error: Unexpected string. this code i used inside javascript. Please have it checked.
Request.Host is providing only the domain name not with domain and virtual directory. Using Request.Host with Request.PathBase providing the correct result as expected.
HttpContext.Request.IsHttps can give you fake result in case you host an app behind load balancing. Better to use Request.Scheme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.