me.at.coding
me.at.coding

Reputation: 17796

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.

Upvotes: 8

Views: 5103

Answers (1)

me.at.coding
me.at.coding

Reputation: 17796

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.

Upvotes: 8

Related Questions