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

发布于 2024-10-31 12:54:23 字数 605 浏览 5 评论 0原文

因此,为了完成我在这篇帖子中提出的要求我执行了以下操作:

    [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

节枝 2024-11-07 12:54:23

您有以下几种选择:

  1. 重定向到您想要返回的操作:return RedirectToAction("Action-I-Want")
  2. 按名称返回视图:return View("The-View-I-Want")

请注意,使用第二种方法时,您必须将视图放在“共享”文件夹中,以便所有控制器都能够找到它并返回它。如果您最终将所有观点都放在那里,这可能会变得混乱。

附带说明:您的工作找不到视图的原因是因为默认视图引擎会在“属于”当前执行控制器上下文的文件夹中查找视图,无论您调用什么代码。

编辑:

可以将所有“移动”视图分组到同一文件夹中。在您的 Global.asax 上(或者在您设置 ViewEngine 的任何地方),只需在 AreaViewLocationFormats 中添加移动视图的路径即可。请注意,您仍然会必须以不同的方式命名您的视图

。我还可以执行一些操作,例如检测浏览器,然后提供正确的文件,例如 View.aspx 和 View.m.aspx

。 ,只需看看 WebFormViewEngine 即可会找出最适合您的方法。

You have a few options:

  1. Redirect to the Action you'd like to return: return RedirectToAction("Action-I-Want").
  2. Return the View by name: 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.

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.

失眠症患者 2024-11-07 12:54:23

将请求发送到由另一个控制器处理的视图的最简单方法是 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.

内心旳酸楚 2024-11-07 12:54:23

实际上,最简单的方法是根据标准编写站点的一个版本,而不是浏览器检测 :D - 然而,为了以更像 ASP.NET mvc 的方式完成它的直接响应,使用:

RedirectToAction("ViewName", "ControllerName"); 

是一种很好的方法我发现如果您觉得必须针对不同的浏览器标准进行编程以在控制器视图下创建主视图和备用“移动”视图,那么它更实用。然后,不要在每个控制器上编写特殊代码,而是像这样扩展控制器。

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

然后修改您的控制器类,改为使用 ControllerExtended 类,只需将一行添加到您具有备用视图的每个操作的顶部,如下所示:

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();
        }            
    }
}

或者,您可以使用 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:

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:

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"); 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文