简体   繁体   English

ASP.NET MVC3 - 如何从另一个控制器提供View()

[英]ASP.NET MVC3 - How to serve View() from another controller

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. 它仍然与此控制器中的Index操作方法具有相同的视图。 Even though I can see it executing the Test.Areas.Mobile.Controllers.HomeController().Index() action method just fine. 即使我可以看到它执行Test.Areas.Mobile.Controllers.HomeController().Index()动作方法就好了。 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)? 如何在不更改请求URL的情况下从Mobile区域提供Index视图(如上面引用的原始帖子中所述)?

You have a few options: 你有几个选择:

  1. Redirect to the Action you'd like to return: return RedirectToAction("Action-I-Want") . 重定向到您要返回的操作: return RedirectToAction("Action-I-Want")
  2. Return the View by name: return View("The-View-I-Want") . 按名称return View("The-View-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 the AreaViewLocationFormats . Mind you, you'll still have to name your views differently. 在您的Global.asax上(或者您在设置ViewEngine ,只需在ViewEngine添加移动视图的AreaViewLocationFormats 。请注意,您仍然需要以不同的方式命名您的视图。

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. 您可以设置View.aspx和View.m.aspx等约定。

Anyhow, just take a look at WebFormViewEngine and you'll figure out what works best for you. 无论如何,只需看看WebFormViewEngine ,你就会找出最适合你的方法。

The easiest way to send a request to a view handled by another controller is RedirectToAction("View-Name", "Controller-Name") . 向另一个控制器处理的视图发送请求的最简单方法是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. View()重载可以获取可能有效的路由信息​​,但是它们需要更多的努力来设置。

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: 实际上,最简单的方法是使您的站点的一个版本在标准而不是浏览器检测上编程:D - 但是直接响应以更多的ASP.NET mvc方式完成它,使用:

RedirectToAction("ViewName", "ControllerName"); 

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. 然后不是在每个控制器上编写特殊代码,而是像这样扩展控制器。

public class ControllerExtended : Controller 
{
    private bool IsMobile = false;
    private void DetectMobileDevices(){ .... }
}

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: 然后修改你的控制器类,改为说出ControllerExtended类,只需将一行添加到你有备用视图的每个Action的顶部,如下所示:

public class ApplicationsController : ControllerExtended
{
    // GET: /Applications/Index
    public ActionResult Index() {
        this.DetectMobileDevices();
        if(this.IsMobile){
            return RedirectToAction("MobileIndex");
        } else {
            // actual action code goes here
            return View();
        }            
    }
}

Alternately you can use return View("ViewName"); 或者你可以使用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) 但根据我的经验,你想要实际执行不同的操作,而不是仅仅在呈现HTML表格的情况下在不同的视图中显示结果,而不是Flex表格来帮助iPhone用户,因为iPhone中没有闪存支持等(撰写本文时)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM