ASP.NET MVC 3 的小记

以下是本人学习 ASP.NET MVC 3 时的总结,很简单,高手慎入。

1. 在母版页 _Layout.cshtml 中定义 @RenderBody(),表示需要在子页面中要替代的。

@{
ViewBag.Title = "ProductList";
Layout = "~/Views/Shared/_Layout.cshtml"; // 指示该页面要使用的母版页,默认为 ~/Views/Shared/_Layout.cshtml
}

 

2. @Html.Partial("_LogOnPartial") 依旧和 MVC 2 一样,表示应用一个 “部分页”

3. 在母版页中可以利用 IsSectionDefined 判断子页面中是否定义了某个 Section,RenderSection 表示呈现子页面中的 Section

母版页:

        <div style="border:1px solid red; text-align:center; background-color:White;">
@if (IsSectionDefined("footer"))
{
@RenderSection("footer", required: false) // required 参数表示当子页面没有定义 footer Section 时,是否抛出异常
}
else
{
<h2>版权:XXX公司</h2>
}
</div>

 

子页面:

@section footer
{
<h2>我的XXX公司的 Index 页面,哈哈</h2>
}

 

4.  IsPost 判断是否是 POST 提交,IsAjax 判断是否是 AJAX 请求。

    @if (IsPost || IsAjax)
    {
        
    }

5.  Request.Unvalidated().Form["ProductDesc"]  表示获取客户端输入的带有 Html 字符串的“产品描述”,如果直接用 Request.Form["ProductDesc"] 将抛出异常:从客户端(ProductDesc="desc<hello></hello>")中检测到有潜在危险的 Request.Form 值。

6.  @wrapper 默认 Html 编码。

        string warpper = "<strong>Bruce说:我是最棒的!</strong>";  /* 原样输出 */
<div>@warpper</div> //默认 @warpper 使用 @System.Web.HttpUtility.HtmlEncode("<strong>Bruce说:我是最棒的!</strong>") 来 Html 编码

 

如果不想编码,

        HtmlString warpper2 = new HtmlString("<strong>我是最棒的!</strong>");  /* 显示粗体文字 */
//或者 RC2版本中的 Html.Raw("<strong>Bruce说:我是最棒的!</strong>") /* 显示粗体文字 */
<div>@warpper2</div>

 

7. @{} 告诉 Razor 模板引擎这里面是 C# or VB 的代码区域,如果想写 html 标签,则直接写就可以了,比如:写 <br />。如果想写普通的文字,则需要把它包含在 <text></text> 中,或者 @: 一下。就表示又告诉 Razor 模板引擎,这里重新回到 文本区域。比如:看下面这个例子:

<div>
@if (true)
{
<br />
@: 欢迎来到MVC 3.0 <br />
@:Razor View Engine @* @* 相当于<text></text> *@
}
</div>
<br /><br />
<div>
@{
string viewEngineName = "Razor";
}
@if (true)
{
@:欢迎来到MVC 3.0 @viewEngineName View Engine
}
</div>

  输出结果:

  欢迎来到MVC 3.0
  Razor View Engine

  欢迎来到MVC 3.0 Razor View Engine



8. @* 这里是注释区域  *@

9.   @() 表示单行 C# or VB 代码。

10. @@ 表示输出一个普通的 @ 字符。

11.  在开发阶段,如果你想调试输出某个类或属性时,请调用 System.Web.Helpers.ObjectInfo.Print ,比如:

    @ObjectInfo.Print(Request.Headers)

12.  @ServerInfo.GetHtml()  表示调用 System.Web.Helpers.ServerInfo.GetHtml ,以显示服务器环境信息。

13.  如果 ViewBag.AllSimpleInfo 是一个 dynamic 类型。

    <h4>以下是产品的简单信息</h4>
<div>
@if (ViewBag.AllSimpleInfo != null)
{
<ul>
@foreach (var item in ViewBag.AllSimpleInfo)
{
<li>@DataBinder.Eval(item, "ProductName") - @DataBinder.Eval(item, "ProduceYear")年</li>
}
</ul>
}
</div>

 

14. 在某个 Controller 中禁用 Session。

    [SessionState(SessionStateBehavior.Disabled)]
public class HomeController : Controller
{

}

15. Action 上面标记 [OutputCache(Duration=6000)]  /* 不再要求 VaryByParam 参数,自动检测 */

16. @functions 的使用

@{
ViewBag.Title = "主页";
}

<h2>@ViewBag.Message</h2>
<p>
若要了解有关 ASP.NET MVC 的更多信息,请访问 <a href="http://asp.net/mvc" title="ASP.NET MVC 网站">http://asp.net/mvc</a>
</p>
<div>
@using System.Text;
@functions
{
public static IHtmlString Repeat<T>(IEnumerable<T> source, Func<T, string> funcCoreContent)
{
StringBuilder builder = new StringBuilder();
foreach (var item in source)
{
string coreContent = funcCoreContent(item);
builder.Append(coreContent);
}
return new HtmlString(builder.ToString());
}
}  
</div>
<div>
@Repeat(new[] { "Bruce", "Jacky", "Rose" }, w => "Hello " + w + "<br/>")
</div>


17. 在 Razor 视图里面获取参数,比如:

@{ 
int empId = Convert.ToInt32(ViewContext.RouteData.Values["employeeId"]);
}

18. ASP.NET MVC 3 中 ActionResult 的子类:


19. 对于 ASP.NET MVC 3 来说,Action 上的参数的默认值是可行的。比如:

        /// <summary>
        /// 删除文章种类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult deleteCategory(int id = 5)
        {
            return Content("删除成功!" + id);
        }

 

20. 关于 RenderBody, RenderPage and RenderSection methods in MVC 3,请看这里:http://www.codeproject.com/Articles/383145/RenderBody-RenderPage-and-RenderSection-methods-in

21. 关于路由的配置,可以参考:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // 忽略处理程序
        routes.IgnoreRoute("Content/{*pathInfo}"); // 忽略静态文件
        routes.IgnoreRoute("m/Content/{*pathInfo}"); // 忽略静态文件
        routes.IgnoreRoute("Scripts/{*pathInfo}"); // 忽略 javascript
        routes.IgnoreRoute("Areas/{*pathInfo}"); // 忽略 areas
        routes.IgnoreRoute("m/Areas/{*pathInfo}"); // 忽略 areas
        routes.IgnoreRoute("{*allgif}", new { allgif = @".*\.gif(/.*)?" }); // 忽略对路径中包含 .gif 的 URL 路由
        routes.IgnoreRoute("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" }); // 忽略对路径中包含 .jpg 的 URL 路由
        routes.IgnoreRoute("{*allpng}", new { allpng = @".*\.png(/.*)?" }); // 忽略对路径中包含 .png 的 URL 路由
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); // 忽略对路径中包含 favicon.ico 的 URL 路由
        routes.IgnoreRoute("{*undefined}", new { undefined = @"(.*/)?undefined(/.*)?" }); // 忽略对路径中包含 undefined 的 URL 路由

        routes.MapRoute(
            "SiteMap", // 路由的名称
            "sitemap", // 路由匹配的 URL
            new { controller = "SiteMap", action = "Index" },  // 默认值
            new string[] { "WebMvcCommon.Controllers" } // 命名空间
        );

 22. 我实在不明白为什么在 MVC 的源码中,竟然还要引用 System.Web.UI,难道是历史遗留问题?


 23.

谢谢浏览!

转载于:https://www.cnblogs.com/Music/archive/2012/03/19/asp-net-mvc-3-my-summary.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值