如何在ASP.NET MVC 3 razor ViewStart文件中指定不同的佈局?

本文翻譯自:How do I specify different Layouts in the ASP.NET MVC 3 razor ViewStart file?

I would like to have 2 separate Layouts in my application. 我希望在我的應用程序中有2個單獨的佈局。 Let say one is for the Public section of the website and the other is for the Member side. 假設一個是網站的公共部分,另一個是會員方面。

For simplicity lets say all the logic for each of theses sites is wrapped neatly into 2 distinct controllers. 爲簡單起見,我們可以說每個站點的所有邏輯都整齊地包含在2個不同的控制器中。

  • PublicController PublicController
  • StaffController StaffController

And that they each have a corresponding Layout for all the View under each. 並且它們每個都具有相應的佈局,用於每個視圖下的所有視圖。

  • _PublicLayout.cshtml _PublicLayout.cshtml
  • _StaffLayout.cshtml _StaffLayout.cshtml

How do I use the _ViewStart.cshtml file to specify that all View's / Action under "Public" use the PublicLayout and everything under "Staff" use the StaffLayout? 如何使用_ViewStart.cshtml文件指定“Public”下的所有View / Action使用PublicLayout,“Staff”下的所有內容都使用StaffLayout?

Thanks! 謝謝!


#1樓

參考:https://stackoom.com/question/Lei4/如何在ASP-NET-MVC-razor-ViewStart文件中指定不同的佈局


#2樓

This method is the simplest way for beginners to control Layouts rendering in your ASP.NET MVC application. 此方法是初學者在ASP.NET MVC應用程序中控制Layouts渲染的最簡單方法。 We can identify the controller and render the Layouts as par controller, to do this we can write our code in _ViewStart file in the root directory of the Views folder. 我們可以識別控制器並將Layouts渲染爲par控制器,爲此,我們可以在Views文件夾的根目錄中的_ViewStart文件中編寫代碼。 Following is an example shows how it can be done. 以下是一個示例,說明如何完成。

  @{
             var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
             string cLayout = "";
            if (controller == "Webmaster") {
                  cLayout = "~/Views/Shared/_WebmasterLayout.cshtml";
                 }
               else {
                cLayout = "~/Views/Shared/_Layout.cshtml";
               }
        Layout = cLayout;
         }

Read Complete Article here "How to Render different Layout in ASP.NET MVC" 閱讀全文在這裏 “如何在ASP.NET MVC呈現不同的佈局”


#3樓

One more method is to Define the Layout inside the View: 另一種方法是在視圖中定義佈局:

   @{
    Layout = "~/Views/Shared/_MyAdminLayout.cshtml";
    }

More Ways to do, can be found here , hope this helps someone. 更多方法可以在這裏找到,希望這有助於某人。


#4樓

You could put a _ViewStart.cshtml file inside the /Views/Public folder which would override the default one in the /Views folder and specify the desired layout: 您可以在/Views/Public文件夾中放置一個_ViewStart.cshtml文件,該文件將覆蓋/Views文件夾中的默認文件,並指定所需的佈局:

@{
    Layout = "~/Views/Shared/_PublicLayout.cshtml";
}

By analogy you could put another _ViewStart.cshtml file inside the /Views/Staff folder with: 通過類比,你可以在/Views/Staff文件夾中放入另一個_ViewStart.cshtml文件:

@{
    Layout = "~/Views/Shared/_StaffLayout.cshtml";
}

You could also specify which layout should be used when returning a view inside a controller action but that's per action: 您還可以指定在控制器操作中返回視圖時應使用的佈局,但這是每個操作:

return View("Index", "~/Views/Shared/_StaffLayout.cshtml", someViewModel);

Yet another possibility is a custom action filter which would override the layout. 另一種可能性是自定義動作過濾器,它將覆蓋佈局。 As you can see many possibilities to achieve this. 你可以看到實現這一目標的許多可能性。 Up to you to choose which one fits best in your scenario. 由您決定哪種方案最適合您的方案。


UPDATE: 更新:

As requested in the comments section here's an example of an action filter which would choose a master page: 根據評論部分的要求,這裏是一個動作過濾器的例子,它將選擇一個母版頁:

public class LayoutInjecterAttribute : ActionFilterAttribute
{
    private readonly string _masterName;
    public LayoutInjecterAttribute(string masterName)
    {
        _masterName = masterName;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
        var result = filterContext.Result as ViewResult;
        if (result != null)
        {
            result.MasterName = _masterName;
        }
    }
}

and then decorate a controller or an action with this custom attribute specifying the layout you want: 然後使用此自定義屬性修飾控制器或操作,指定所需的佈局:

[LayoutInjecter("_PublicLayout")]
public ActionResult Index()
{
    return View();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章