ASP.Net MVC:不同控制器中的相同动作名称

时间:2011-09-07 09:27:42

标签: asp.net-mvc

我有2个控制器,分别是SearchController和SearchByStaffController。它们非常相似,都有一个动作名称为“搜索”的动作。当我在他们的普通超级类中调用View(“搜索”)时,会出现混乱。仅呈现带有SearchController的“搜索”视图。

  1. MVC框架是否只获得与名称匹配的第一个视图而忽略其余视图?
  2. 我试图在View()中传递视图路径并且它有效。这样做会有副作用吗?我在网上搜索过,似乎没有人这样做过。
  3. 谢谢!

2 个答案:

答案 0 :(得分:0)

user932390,

mvc使用约定优于配置。这意味着“搜索”视图必须位于:

views/Search 

views/SearchByStaff
分别是

文件夹。解决这个问题的唯一方法是在views/shared文件夹下找到搜索视图,然后视图引擎会在两种情况下找到它并使用它(假设它们具有相同的模型)。

答案 1 :(得分:0)

Does the MVC framework get only the first view that matches the name and ignore the rest?

是。路由规则是自然的(从上到下),当规则匹配时,所有结束。

I tried to pass the view path in View() and it worked. Would there be any side effect for doing so? I searched over the web and seems no one has done this before.

你可以但我不喜欢这样,因为MVC基于惯例。因此,我认为强迫视图的路径是打破约定的一种方式。您确定不能简单地为这两种方法创建两个路由规则吗?所以你可以这样做:

return RedirectToAction("Search", "Controller1");

return RedirectToAction("Search", "Controller2");