MVC4 - ContextDependentView - 它是什么意思?

Sam*_*tar 10 asp.net asp.net-mvc asp.net-mvc-4

我刚开始使用MVC4,我看到的第一个动作方法有新的东西.我检查了互联网,但无法找到任何相关信息:

public ActionResult LogOn()
        {
            return ContextDependentView();
        }
Run Code Online (Sandbox Code Playgroud)

有谁知道ContextDependentView是什么?

对我来说是新事物.

one*_*mer 11

其目的是为登录和注册操作提供View或PartialView操作结果.

    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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

像MVC中的其他东西一样,它是通过约定来完成的......这里的约定是当Request.QueryString包含a时?content=xxxx,它将"Json"添加到动作名称,将其填充为ViewBag属性并返回View的部分版本.例如:

请求/Account/Login?content=test将被解析为ViewBag.FormAction = "JsonLogin";然后返回部分.

请求/Account/Login没有内容查询字符串,因此其表单操作仍然存在ViewBag.FormAction = "Login";