1

I have a problem in url in my asp.net mvc application : i have two controllers with two actions. in the controller Client

  public ActionResult Index(string path)
        {
            if (CompteModels.Connected)
            {
                /*
                ProjetModels projets = new ProjetModels();
                List<string> _noms_de_projets = projets.GetProjectsFromClient(CompteModels.Id_connected);
                return View(_noms_de_projets);
              * */

                string realPath;
                realPath = "C:/Projets/" + path;
                realPath = realPath.Replace("Index/", "");

                if (System.IO.File.Exists(realPath))
                {

                    return base.File(realPath, "application/octet-stream");
                }
                else if (System.IO.Directory.Exists(realPath))
                {

                    Uri url = Request.Url;

                    if (url.ToString().Last() != '/')
                    {
                        Response.Redirect("/Client/Index" + path + "/");
                    }

                    List<DirModel> dirListModel = new List<DirModel>();

                    IEnumerable<string> dirList = Directory.EnumerateDirectories(realPath);
                    foreach (string dir in dirList)
                    {
                        DirectoryInfo d = new DirectoryInfo(dir);

                        DirModel dirModel = new DirModel();

                        dirModel.DirName = Path.GetFileName(dir);
                        dirModel.DirAccessed = d.LastAccessTime;

                        dirListModel.Add(dirModel);
                    }


                    List<FileModel> fileListModel = new List<FileModel>();

                    IEnumerable<string> fileList = Directory.EnumerateFiles(realPath);
                    foreach (string file in fileList)
                    {
                        FileInfo f = new FileInfo(file);

                        FileModel fileModel = new FileModel();

                        if (f.Extension.ToLower() != "php" && f.Extension.ToLower() != "aspx"
                            && f.Extension.ToLower() != "asp")
                        {
                            fileModel.FileName = Path.GetFileName(file);
                            fileModel.FileAccessed = f.LastAccessTime;
                            fileModel.FileSizeText = (f.Length < 1024) ? f.Length.ToString() + " B" : f.Length / 1024 + " KB";

                            fileListModel.Add(fileModel);
                        }
                    }

                    ExplorerModel explorerModel = new ExplorerModel(dirListModel, fileListModel);

                    return View(explorerModel);
                }
                else
                {
                    return Content(path + " is not a valid file or directory.");
                }
            }

            else return RedirectToAction("Login", "Account");
        }

the result is: cli

but in the controller Akeo

 public ActionResult Index(string path)
        {
            if (CompteModels.Connected)
            {
                /*
                ProjetModels projets = new ProjetModels();
                List<string> _noms_de_projets = projets.GetProjectsFromClient(CompteModels.Id_connected);
                return View(_noms_de_projets);
              * */

                string realPath;
                realPath = "C:/Projets/" + path;
              realPath = realPath.Replace("Index/", "");

                if (System.IO.File.Exists(realPath))
                {

                    return base.File(realPath, "application/octet-stream");
                }
                else if (System.IO.Directory.Exists(realPath))
                {

                    Uri url = Request.Url;

                    if (url.ToString().Last() != '/')
                    {
                        Response.Redirect("/Akeo/Index" + path + "/");
                    }

                    List<DirModel> dirListModel = new List<DirModel>();

                    IEnumerable<string> dirList = Directory.EnumerateDirectories(realPath);
                    foreach (string dir in dirList)
                    {
                        DirectoryInfo d = new DirectoryInfo(dir);

                        DirModel dirModel = new DirModel();

                        dirModel.DirName = Path.GetFileName(dir);
                        dirModel.DirAccessed = d.LastAccessTime;

                        dirListModel.Add(dirModel);
                    }


                    List<FileModel> fileListModel = new List<FileModel>();

                    IEnumerable<string> fileList = Directory.EnumerateFiles(realPath);
                    foreach (string file in fileList)
                    {
                        FileInfo f = new FileInfo(file);

                        FileModel fileModel = new FileModel();

                        if (f.Extension.ToLower() != "php" && f.Extension.ToLower() != "aspx"
                            && f.Extension.ToLower() != "asp")
                        {
                            fileModel.FileName = Path.GetFileName(file);
                            fileModel.FileAccessed = f.LastAccessTime;
                            fileModel.FileSizeText = (f.Length < 1024) ? f.Length.ToString() + " B" : f.Length / 1024 + " KB";

                            fileListModel.Add(fileModel);
                        }
                    }

                    ExplorerModel explorerModel = new ExplorerModel(dirListModel, fileListModel);

                    return View(explorerModel);
                }
                else
                {
                    return Content(path + " is not a valid file or directory.");
                }
            }

            else return RedirectToAction("Login", "Account");
        }

the result is an exception like this: ak

the views are:

v

So what is the reason of this difference between the results? and how can i avoid this error?

2 Answers 2

2

The two actions are identical except for this line:

Response.Redirect("/Client/Index" + path + "/");

vs.

Response.Redirect("/Akeo/Index" + path + "/");

so I doubt there's much wrong with the code itself.

You're not getting an Exception from the code, you're getting a 404 error from IIS, as in "Page Not Found". Check that you have an Index view file in both a Client folder and an Akeo folder underneath your parent Views folder and that you're calling your actions with the correct name.

Sign up to request clarification or add additional context in comments.

1 Comment

MVC may be getting confused between you browsing to a path and calling the Index action. Can you post your route configuration?
1

In first controller it is looking for a view : dotPeek-1.0..2545 in Views\Client\Index Folder. second controller looking for view : dotPeek-1.0..2545 in Views\Akeo\Index Folder.

see if you have this file in second folder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.