使用asp.net mvc应用程序编写URL

时间:2013-06-05 12:35:56

标签: c# asp.net .net asp.net-mvc uri

我的asp.net mvc应用程序中url出现问题: 我有两个控制器有两个动作。 在控制器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");
        }

结果是: cli

但在控制器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");
        }

结果是这样的异常: ak

观点是:

v

那么结果之间存在这种差异的原因是什么?我该如何避免这个错误?

2 个答案:

答案 0 :(得分:2)

除了这一行,这两个动作是相同的:

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

VS

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

所以我怀疑代码本身有多大的错误。

您没有从代码中获得Exception,您从IIS收到404错误,如“未找到页面”。检查您的父Index文件夹下的Client文件夹和Akeo文件夹中是否有Views个视图文件,并且您使用正确的名称调用了您的操作

答案 1 :(得分:1)

在第一个控制器中,它正在Views \ Client \ Index文件夹中查找视图:dotPeek-1.0..2545。 第二个控制器在Views \ Akeo \ Index Folder中寻找视图:dotPeek-1.0..2545。

查看您是否在第二个文件夹中包含此文件