-->

MVC4 - ContextDependentView - What does it mean?

2020-08-17 08:19发布

问题:

I have just started using MVC4 and the first action method that I look at has something new. I checked out the internet and can't find anything about this:

public ActionResult LogOn()
        {
            return ContextDependentView();
        }

Does anyone know what a ContextDependentView is ?

Something new to me.

回答1:

It's purpose is to facilitate View or PartialView action results for the Login and Register actions.

    private ActionResult ContextDependentView()
    {
        string actionName = ControllerContext.RouteData.GetRequiredString("action");
        if (Request.QueryString["content"] != null)
        {
            ViewBag.FormAction = "Json" + actionName;
            return PartialView();
        }
        else
        {
            ViewBag.FormAction = actionName;
            return View();
        }
    }

Like other things in MVC it is done by convention... the convention here is when the Request.QueryString contains a ?content=xxxx, it prepends "Json" to the action name, stuffs it a ViewBag property and returns the partial version of the View. For example:

A request for /Account/Login?content=test would be resolved to ViewBag.FormAction = "JsonLogin"; and then return a partial.

A request for /Account/Login has no content query string so its form action remains ViewBag.FormAction = "Login";