Search code examples
razorasp.net-core-mvc

Request does not exist in the razor pages


I'm trying this solution to use URL among my razor view.

but unfortunately I have this compiler error:

the name 'Request' does not exist in the current context

I tried this solution too, but still HttpContext.Current.Request is unknown for compiler!

SOLUTION:

I used @Context.Request.Path and the issue solved. thanks to Xinran Shen


Solution

  • In your razor view, You use:

    @Context.Request.Path
    

    to get the current request path directly. If you wanna get the whole request path(include host), you can use

    @using Microsoft.AspNetCore.Http.Extensions
    
    @Context.Request.GetEncodedUrl()
    

    enter image description here

    enter image description here

    Hope it can solve your issue.