Grab URL directly from an image of my ASP NET MVC application

Asked

Viewed 400 times

2

Good afternoon, I’m trying to make an API to return the URL of an image list but I don’t know how to do it.

I found things with server.mapPatch but these return the physical address of the file.

I wanted to return the type URL like this:

www.meusite.com.br/conteudo/categoria/foto.jpg

I did so but returns me the physical path, wanted mine URL

public JsonResult Fotos()
{
    DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/Content/imagens/categoria"));
    var lista = new List<string>();
    foreach (var item in dirInfo.GetFiles())
    {
        lista.Add(item.FullName);
    }
    return Json(lista, JsonRequestBehavior.AllowGet);
}

I managed to do it this way but it got ugly, it seems like I was :C

var dominio = "http://localhost:53592";
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/Content/imagens/categoria"));
var lista = new List<string>();
foreach (var item in dirInfo.GetFiles())
{
    lista.Add(dominio + "/Content/imagens/categoria/" + item.Name);
}
return Json(lista, JsonRequestBehavior.AllowGet);

1 answer

2


I’d do it this way, buddy:

string dominio = Path.Combine(Request.Url.Scheme, Request.Url.Authority);

DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/Assets/img/icons/led"));
List<string> lista = dirInfo.GetFiles().Select(x => Path.Combine(dominio + "\\Content\\imagens\\categoria\\", x.Name)).ToList();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.