开发者

ASP.Net MVC: same action name in different controllers

I have 2 controllers which are SearchController and SearchByStaffController respectively. They are very similar and both have an action with action name "Search". When I call View("Search") in their common super class, the confusion comes. Only the "Search" view with SearchController is rendered.

  1. Does the MVC framework get only the first view that matches the name and ignore the rest?
  2. I tried to pass 开发者_JS百科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.

Thanks!


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

Yes. The routing rules are aparsed (top to bottom) and when a rule is matched all end.

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.

You can but I don't like that because MVC is based on conventions. So, I see forcing the path of the view a way to broke a convention. Are you sure you can't simply create two routing rules for the two methods? So you can do something like this:

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

and

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


user932390,

mvc uses convention over configuration. this means that the 'search' view will have to be located in both the:

views/Search 

and

views/SearchByStaff

folders respectively. the only way around this is to locate the search view under the views/shared folder, then the viewengine will find it there in both cases and use it (assuming they have the same model).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜