How to get current url in view cshtml in core

Dung Do Tien Apr 05 2020 3171

Before I coded with .Net Framework to get the current request in view(cshtml)  I use to bellow command:

 <div class="url">
 @HttpContext.Current.Request.Path </div>

But now I coding with Asp.Net Core 2.2 HttpContext is not found. What command is similar in .net core?

Have 1 answer(s) found.
  • S

    Sandeep Kumar Apr 05 2020

    It is simple to get the current URL in view (cshtml). You can use the command below :



    Create property and get the current URL from the controller and binding it to view.

     public ActionResult Index()
    	CommentModel model = new CommentModel();
    	model.CurrentUrl = Request.Path;
    	return View(model);

    and in view

     @model CommentModel
    @if (Model != null)
        <div class="url">
            @Model.CurrentUrl </div>
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close