Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between HttpContext.Request.Path and HttpContext.Request.PathBase?

I am wondering what's the difference between HttpContext.Request.Path and HttpContext.Request.PathBase in a Web API controller? I read the documentation but didn't understand what the intented difference should be, even after testing both properties:

public async Task<ActionResult<string>> PostItem(ItemPostRequest itemPostRequest)
{   
    // Output: Path is: '/api/items'
    Debug.WriteLine($"Path is: '{HttpContext.Request.Path}'");
    
    // Output: PathBase is: ''
    Debug.WriteLine($"PathBase is: '{HttpContext.Request.PathBase}'");

    // [...]
}

When would PathBase be non-empty? I am on NET 5.0.

like image 887
stefan.at.wpf Avatar asked Apr 29 '26 00:04

stefan.at.wpf


1 Answers

As Camilo wrote, it's about app.UsePathBase("/some-path").

Adding app.usePathBase("/mysite1") one needs to call /mysite1/api/items instead of /api/items and then it looks like this:

Path is: '/api/items'
PathBase is: '/mysite1'

Obviously PathBase can be used to host multiple sites/APIs on one host.

like image 169
stefan.at.wpf Avatar answered Apr 30 '26 15:04

stefan.at.wpf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!