简体   繁体   中英

Writing url with an asp.net mvc application

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM