是否可以将多个动作分配给同一个控制器方法?
Is it possible to assign multiple actions to the same controller method?
我有一个简单的 MVC 控制器,它 returns 基于 View 操作的文件夹中的文件列表。 Index()
操作包含一个集合列表,因此用户点击 CollectionOne
,就会填充相应的视图。相同的行为适用于其他集合。
问题是,我有很多冗余代码,我已经能够通过使用所有操作调用的私有 ActionContent()
方法在一定程度上进行管理。因此,每当我有一个新的集合要添加到网站时,我只需为该集合添加一个 ActionResult
并调用 ActionContent()
方法。
有什么方法可以优化此代码以使其更具动态性,而无需在每次需要 post 新集合时添加新的 ActionResult
?
我的控制器是这样的:
public class PortfolioController : Controller
{
public ActionResult CollectionOne()
{
return View(ActionContent());
}
public ActionResult CollectionTwo()
{
return View(ActionContent());
}
private IEnumerable<string> ActionContent()
{
const string folder = @"~/Content/images/portfolio/";
var path = folder + ControllerContext.RouteData.Values["action"];
var files = Directory
.EnumerateFiles(Server.MapPath(path))
.Select(Path.GetFileName);
return files;
}
}
我想使用 ActionNames
或适当的 route mapping
:
将它变成这样的东西(以避免冗余)
public class PortfolioController : Controller
{
[ActionName("CollectionOne")]
[ActionName("CollectionTwo")]
[ActionName("CollectionThree")]
public ActionResult PortfolioCollection()
{
const string folder = @"~/Content/images/portfolio/";
var path = folder + ControllerContext.RouteData.Values["action"];
var files = Directory
.EnumerateFiles(Server.MapPath(path))
.Select(Path.GetFileName);
return View(files);
}
}
这就是参数的用途:
public ActionResult PortfolioCollection(string id)
{
const string folder = @"~/Content/images/portfolio/";
var files = Directory
.EnumerateFiles(Server.MapPath(folder + id))
.Select(Path.GetFileName);
return View(files);
}
您可以制作自定义路线以分配您想要的任何 URL 模式。
我有一个简单的 MVC 控制器,它 returns 基于 View 操作的文件夹中的文件列表。 Index()
操作包含一个集合列表,因此用户点击 CollectionOne
,就会填充相应的视图。相同的行为适用于其他集合。
问题是,我有很多冗余代码,我已经能够通过使用所有操作调用的私有 ActionContent()
方法在一定程度上进行管理。因此,每当我有一个新的集合要添加到网站时,我只需为该集合添加一个 ActionResult
并调用 ActionContent()
方法。
有什么方法可以优化此代码以使其更具动态性,而无需在每次需要 post 新集合时添加新的 ActionResult
?
我的控制器是这样的:
public class PortfolioController : Controller
{
public ActionResult CollectionOne()
{
return View(ActionContent());
}
public ActionResult CollectionTwo()
{
return View(ActionContent());
}
private IEnumerable<string> ActionContent()
{
const string folder = @"~/Content/images/portfolio/";
var path = folder + ControllerContext.RouteData.Values["action"];
var files = Directory
.EnumerateFiles(Server.MapPath(path))
.Select(Path.GetFileName);
return files;
}
}
我想使用 ActionNames
或适当的 route mapping
:
public class PortfolioController : Controller
{
[ActionName("CollectionOne")]
[ActionName("CollectionTwo")]
[ActionName("CollectionThree")]
public ActionResult PortfolioCollection()
{
const string folder = @"~/Content/images/portfolio/";
var path = folder + ControllerContext.RouteData.Values["action"];
var files = Directory
.EnumerateFiles(Server.MapPath(path))
.Select(Path.GetFileName);
return View(files);
}
}
这就是参数的用途:
public ActionResult PortfolioCollection(string id)
{
const string folder = @"~/Content/images/portfolio/";
var files = Directory
.EnumerateFiles(Server.MapPath(folder + id))
.Select(Path.GetFileName);
return View(files);
}
您可以制作自定义路线以分配您想要的任何 URL 模式。