ASP.NET MVC3 - 如何从另一个控制器提供 View() 服务
因此,为了完成我在这篇帖子中提出的要求我执行了以下操作:
[iPhone]
[ActionName("Index")]
public ActionResult IndexIPhone()
{
return new Test.Areas.Mobile.Controllers.HomeController().Index();
}
[ActionName("Index")]
public ActionResult Index()
{
return View();
}
它仍然提供与此控制器中的 Index 操作方法相同的视图。尽管我可以看到它很好地执行 Test.Areas.Mobile.Controllers.HomeController().Index()
操作方法。这是怎么回事?如何在不更改请求 URL 的情况下从移动区域提供索引视图(如上面引用的原始帖子中所述)?
So in order accomplish what I asked in this post I did the following:
[iPhone]
[ActionName("Index")]
public ActionResult IndexIPhone()
{
return new Test.Areas.Mobile.Controllers.HomeController().Index();
}
[ActionName("Index")]
public ActionResult Index()
{
return View();
}
Which still serves the same view as the Index action method in this controller. Even though I can see it executing the Test.Areas.Mobile.Controllers.HomeController().Index()
action method just fine. What's going on here? And how do I serve the Index view from Mobile area without changing the request URL (as asked in the original post referenced above)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有以下几种选择:
return RedirectToAction("Action-I-Want")
。return View("The-View-I-Want")
。请注意,使用第二种方法时,您必须将视图放在“共享”文件夹中,以便所有控制器都能够找到它并返回它。如果您最终将所有观点都放在那里,这可能会变得混乱。
附带说明:您的工作找不到视图的原因是因为默认视图引擎会在“属于”当前执行控制器上下文的文件夹中查找视图,无论您调用什么代码。
编辑:
可以将所有“移动”视图分组到同一文件夹中。在您的 Global.asax 上(或者在您设置
ViewEngine
的任何地方),只需在AreaViewLocationFormats
中添加移动视图的路径即可。请注意,您仍然会必须以不同的方式命名您的视图。我还可以执行一些操作,例如检测浏览器,然后提供正确的文件,例如 View.aspx 和 View.m.aspx
。 ,只需看看 WebFormViewEngine 即可会找出最适合您的方法。
You have a few options:
return RedirectToAction("Action-I-Want")
.return View("The-View-I-Want")
.Note that with the 2nd approach you'd have to put your view in the "Shared" folder for all controllers to be able to find it and return it. This can get messy if you end up putting all your views there.
As a side note: The reason your work doesn't find the view is because default view engine looks for the view in the folder that "belongs" to the current executing controller context, regardless of what code you're calling.
Edit:
It is possible to group all "mobile" views in the same folder. On your Global.asax (or where ever you're setting up your
ViewEngine
, just add the path to your mobile View in theAreaViewLocationFormats
. Mind you, you'll still have to name your views differently.You can also write your own view engine. I'd do something like detecting the browser and then serving the right file. You could setup a convention like View.aspx, and View.m.aspx.
Anyhow, just take a look at WebFormViewEngine and you'll figure out what works best for you.
将请求发送到由另一个控制器处理的视图的最简单方法是
RedirectToAction("View-Name", "Controller-Name")
。View()
的重载也可以获取路线信息,但需要更多的精力来设置。The easiest way to send a request to a view handled by another controller is
RedirectToAction("View-Name", "Controller-Name")
.There are overloads of
View()
that take route information that might work as well, but they'd require more effort to set up.实际上,最简单的方法是根据标准编写站点的一个版本,而不是浏览器检测 :D - 然而,为了以更像 ASP.NET mvc 的方式完成它的直接响应,使用:
是一种很好的方法我发现如果您觉得必须针对不同的浏览器标准进行编程以在控制器视图下创建主视图和备用“移动”视图,那么它更实用。然后,不要在每个控制器上编写特殊代码,而是像这样扩展控制器。
然后修改您的控制器类,改为使用 ControllerExtended 类,只需将一行添加到您具有备用视图的每个操作的顶部,如下所示:
或者,您可以使用 return View("ViewName");但根据我的经验,您希望实际执行不同的操作,而不是仅仅在不同的视图中显示结果,就像呈现 HTML 表格而不是 Flex 表格来帮助 iPhone 用户一样,因为 iPhone 中不支持 Flash等(截至撰写本文时)
Well actually the easiest way is to make one version of your site programmed on standards instead of browser detection :D -- however in direct response to accomplish what it in a more of a ASP.NET mvc fashion, using:
is a good method however I have found it is more practical if you feel you must program for different browser standards to create a primary view and an alternate "mobile" view under your controllers views. Then instead of writing special code on every controller, instead extend the controller like so.
Then modify your controller classes to instead say ControllerExtended classes and just add the one line to the top of each Action that you have alternate views of like so:
Alternately you can use return View("ViewName"); but from my experience you want to actually perform different actions as opposed to just showing the result in a different view as in the case of presenting an HTML table as opposed to a Flex table to help iPhone users since there is no flash support in the iPhone, etc. (as of this writing)