2

I'm request for get url :

  public Uri GetAbsoluteUri()
    {
        var request = _httpContextAccessor.HttpContext.Request;
        UriBuilder uriBuilder = new UriBuilder();
        uriBuilder.Scheme = request.Scheme;
        uriBuilder.Host = request.Host.Host;
        uriBuilder.Path = request.Path.ToString();
        uriBuilder.Query = request.QueryString.ToString();
        return uriBuilder.Uri;
    }

    public string RootPath => Path.Combine(WebRootPath, RootFolderName);

    public string GetProductPicturePath()
    {
        return Path.Combine(GetAbsoluteUri().ToString(), RootFolderName, ProductPictureFolder);
    }

    public string GetProductMainPicturePath()
    {
        string path = Path.Combine(GetAbsoluteUri().ToString(), RootFolderName, ProductPictureFolder, ProductMainPictureFolder);
        return path;
    }

    public string GetNewPath()
    {
        string productMainPicturePath = GetProductMainPicturePath();
        return Path.Combine(productMainPicturePath);
    }

finally i using the GetNewPath().

, but this will give me the address :

https://localhost/api/Product/GetProductList/Upload/ProductPictureFolder/ProductMainPicture/77777.png

but i have 2 problem with this url :

1 - it not contain port in url https://localhost/api but i need return like this : http://localhost:4200/api

2 - This includes the name of the controller and the ActionName, but I need to like this : https://localhost/Upload/ProductPictureFolder/ProductMainPicture/77777.png

but it return for me this : https://localhost/api/Product/GetProductList/Upload/ProductPictureFolder/ProductMainPicture/77777.png

i not need this /api/Product/GetProductList .

Product : Controller Name

GetProductList : ActionName

How Can I Solve This Problem ???

3
  • can you post exactly how you want your url to be.
    – David
    Jan 29, 2019 at 15:20
  • @David i need this https://localhost:44390/Upload/ProductPictureFolder/ProductMainPicture/77777.png Jan 29, 2019 at 15:23
  • @David it not working Jan 29, 2019 at 15:43

1 Answer 1

3

1 - it not contain port in url https://localhost/api but i need return like this

To get port you can use this snippet :

if (request.Host.Port.HasValue) 
   uriBuilder.Port = request.Host.Port.Value;

2 - This includes the name of the controller and the ActionName, but I need to like this : https://localhost/Upload/ProductPictureFolder/ProductMainPicture/77777.png

I suggest you that set UriBuilder Path based on your needs, not from the request. Something like this:

// Make your Upload file path here
var relativePath = Path.Combine(folderName, filename);

var request = _httpContextAccessor.HttpContext.Request;
var uriBuilder = new UriBuilder
{
    Host = request.Host.Host,
    Scheme = request.Scheme,
    Path = relativePath
};

if (request.Host.Port.HasValue) 
    uriBuilder.Port = request.Host.Port.Value;

var imageUrl = uriBuilder.ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.