Part 60 - ViewStart in asp.net mvc
来源:互联网 发布:电脑无法连接网络wifi 编辑:程序博客网 时间:2024/05/17 23:31
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.
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.
- Part 60 - ViewStart in asp.net mvc
- Part 86 - ValidationSummary in asp.net mvc
- Part 2 - View engines in asp.net mvc
- Part 54 - T4 templates in asp.net mvc
- Part 81 - Range attribute in asp.net mvc
- Part 82 - Creating custom validation attribute in asp.net mvc
- Part 83 - RegularExpression attribute in asp.net mvc
- Part 84 - Compare attribute in asp.net mvc
- Part 85 - Enable client side validation in asp.net mvc
- Part 88 - Unobtrusive validation in asp.net mvc
- Part 89 - Remote validation in asp.net mvc
- ASP.NET MVC Framework (Part 1)
- 【转】ASP.NET MVC Framework (Part 1)
- 【转】ASP.NET MVC Framework (Part 1)
- Asp.Net MVC 视图(概述) - Part.1
- Asp.Net MVC 视图(概述) - Part.1
- AOP in Asp.net MVC
- AOP in Asp.net MVC
- VPNService 一串错误代码
- @Scheduled注解 定时任务 cron表达式详解
- Linux定时调度shell脚本
- Label自适应高度与长度
- Xshell使用root账户连接Deepin(Ubuntu)
- Part 60 - ViewStart in asp.net mvc
- 欢迎使用CSDN-markdown编辑器
- 微信小程序学习:使用picker封装省市区三级联动模板
- 内存分配策略
- 多媒体封装格式学习:H264封装成FLV(一)
- Xcode8 错误:“xx”is missing from working copy
- draw9patch工具使用
- Ext 显示金钱样式
- 永久勘误:微软等面试100题系列,答案V0.4版[第41-60题答案]