我试图实现用户友好的URL,同时保留现有的路线,并能够使用顶部的ActionName标签我的控制器(Can you overload controller methods in ASP.NET MVC?)如何在ASP.NET MVC中共享2个控制器的控制器逻辑,在那里他们被覆盖
的我有2个控制器:
ActionName("UserFriendlyProjectIndex")]
public ActionResult Index(string projectName) { ... }
public ActionResult Index(long id) { ... }
基本上,我试图做的是我存放用户友好的URL数据库中的每个项目。
如果用户输入的URL /项目/ TopSecretProject/,动作UserFriendlyProjectIndex被调用。我做一个数据库查找,如果一切都检出,我想要应用在索引操作中使用的完全相同的逻辑。
我基本上是为了避免编写重复的代码。我知道我可以将通用逻辑分解成另一种方法,但我想知道在ASP.NET MVC中是否有内置的方法。
有什么建议吗?
我尝试以下,我去查看找不到错误消息:
[ActionName("UserFriendlyProjectIndex")]
public ActionResult Index(string projectName)
{
var filteredProjectName = projectName.EscapeString().Trim();
if (string.IsNullOrEmpty(filteredProjectName))
return RedirectToAction("PageNotFound", "Error");
using (var db = new PIMPEntities())
{
var project = db.Project.Where(p => p.UserFriendlyUrl == filteredProjectName).FirstOrDefault();
if (project == null)
return RedirectToAction("PageNotFound", "Error");
return View(Index(project.ProjectId));
}
}
这里的错误消息:
The view 'UserFriendlyProjectIndex' or its master could not be found. The following locations were searched:
~/Views/Project/UserFriendlyProjectIndex.aspx
~/Views/Project/UserFriendlyProjectIndex.ascx
~/Views/Shared/UserFriendlyProjectIndex.aspx
~/Views/Shared/UserFriendlyProjectIndex.ascx
Project\UserFriendlyProjectIndex.spark
Shared\UserFriendlyProjectIndex.spark
我用SparkViewEngine作为视图引擎和LINQ如果有帮助的话 谢谢!