Part 60 - ViewStart in asp.net mvc

 To associate a view with a layout file, we have to set Layout property on each and every view. This violates DRY (Don't Repeat Yourself) principle, and has the following disadvantages
1. Redundant code.

2. Maintenance overhead. To use a different layout file, all the views need to be updated.

What is _ViewStart.cshtml file?
ASP.NET MVC 3, has introduced _ViewStart.cshtml. Specify the Layout property in this file and place it in the Views folder. All the views will then use the layout file that is specified in _ViewStart.cshtml. This eliminates the need to specify Layout property on each and every view, and making them more cleaner and maintainable.
_ViewStart in asp.net mvc 3 

If you want a set of views in a specific folder, to use a different layout file, then you can include another _ViewStart.cshtml file in that specific folder. 

When I use _ViewStart.cshtml file, can I still set Layout property on individual views?
Yes, if you want to use a layout file that is different from what is specified in _ViewStart.cshtml

Where else can I specify a layout file?
Layout file can also be specified in a controller action method or in an action filter.

In Controller Action Method:
Specify which layout to use  when returning a view inside a controller action
public ActionResult Create()
{
    return View("Create", "_Layout");
}

We will discuss action filters in a later video session.

Can we write some logic in "_ViewStart.cshtml" to dynamically specify which layout file to use?
Yes, the following code will change the layout file to use based on the browser type. 
If the browser is google chrome, 
    then "_Layout.cshtml" layout file is used
Else
    "_DifferentLayout.cshtml" layout file is used

Code in "_ViewStart.cshtml" file
@{
    Layout = Request.Browser.IsBrowser("Chrome") ? "~/Views/Shared/_Layout.cshtml" :"~/Views/Shared/_DifferentLayout.cshtml" ;
}

All partial views in my application are now using the layout file specified in "_ViewStart.cshtml". How do I prevent these partial views from using a layout file?
Details action method below, returns "_Employee" partial view, and is using the layout file specified in "_ViewStart.cshtml"
public ActionResult Details(int id)
{
    Employee employee = db.Employees.Single(e => e.Id == id);
    return View("_Employee", employee);
}

To prevent this partial view from using the layout file, specified in "_ViewStart.cshtml", return "PartialViewResult" from the controller action method as shown below.
public PartialViewResult Details(int id)
{
    Employee employee = db.Employees.Single(e => e.Id == id);
    return PartialView("_Employee", employee);
}

What will be the layout file extension, if VB.NET is my programming language?
.vbhtml

In our next video, we will discuss using named sections in a layout file. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值