我的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");
}
结果是:
但在控制器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");
}
结果是这样的异常:
观点是:
那么结果之间存在这种差异的原因是什么?我该如何避免这个错误?
答案 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。
查看您是否在第二个文件夹中包含此文件