-1

I have following method to get an Image path in a Project

    public ActionResult RenderImage(string imageid, string pathvalue)
    {
        try
        {
            var URL = System.Configuration.ConfigurationManager.AppSettings[pathvalue].ToString();
            var path = Path.Combine(URL, imageid + ".jpg");
            return base.File(path, "image/jpeg");
        }
        catch (Exception)
        {
            throw;
        }
    }

I want to take this returning Image path as string , for that I tried something like this

ControllerName ns = new ControllerName ();

var path = ns .RenderImage("id", "path");   
string imageurl = path.ToString();

but this is not getting image path as "C:/User/Data/Image.jpg" its getting value as "System.Web.Mvc.FilePathResult"

How to solve this

kez
  • 2,273
  • 9
  • 64
  • 123
  • Did you take a look at what methods are available to `System.Web.Mvc.FilePathResult` namely the [`Filename`](https://msdn.microsoft.com/en-us/library/system.web.mvc.filepathresult.filename(v=vs.118).aspx#P:System.Web.Mvc.FilePathResult.FileName) method. – Mark Hall Nov 17 '16 at 05:53
  • Why are you calling a controller method that returns a `FileResult` if you just want a string? What is it that your really trying to do here? And if its related to [your previous question](http://stackoverflow.com/questions/40646958/how-to-call-controller-action-method-from-partial-view), then why are you not setting the corret path in the first place using `Url.Content()` –  Nov 17 '16 at 06:24
  • @StephenMuecke I'm try to get exact image url as model property object – kez Nov 17 '16 at 06:30
  • So just format the property in the first place using `Path.Combine(path, imageid + ".jpg");` when you pass the model to the view. The answer you have accepted is nonsense. And if you do want to seriously degrade performance by calling a server method each time `User` in your collection, then all you need to `return Content(Path.Combine(path, imageid + ".jpg"));` - creating a path to read and return a file and then just get the its filename and throw away the file makes no sense at all. –  Nov 17 '16 at 06:38

1 Answers1

3

Try:

var ns = new ControllerName ();

var path = ns.RenderImage("id", "path");  

 var fileResult = path  as FilePathResult;

    if (fileResult != null)
    {

        string imageurl = fileResult.FileName;
        imageurl = imageurl.Replace(@"\\", @"\");
    }
Mate
  • 4,976
  • 2
  • 32
  • 38
  • your answer is seems correct , but as imageurl I'm getting something like this `"D:\\Folder\\Location\\A.jpg"` how to remove "\\" form above and get url like this `"D:\Folder\Location\A.jpg"` – kez Nov 17 '16 at 06:09
  • @kez updated . Check http://stackoverflow.com/questions/7482360/replace-with-in-a-string-in-c-sharp for more details – Mate Nov 17 '16 at 06:12
  • Great. I'm glad it helps – Mate Nov 17 '16 at 06:22