大佬教程收集整理的这篇文章主要介绍了Asp.net mvc 知多少(三),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Q24. ASP.NET MVC中主要的命名空间有哪些? Ans. 主要有以下三个比较重要的命名空间:
Q25. 什么是视图引擎? Ans. 视图引擎作为mvc的子系统拥有自身的语义标记。它的职责是转换服务器模板为html标记并渲染呈现到浏览器。最初,ASP.mvc只有一种视图引擎web forms(aspx) ,从ASP.NET MVC3 引入了新的Razor视图引擎。除此之外,还有 Spark,NHaml 等第三方视图引擎。
Q26. 视图引擎是怎样工作的? Ans. 每种视图引擎主要包括以下三个主要部分:
IViewENGIne
接口;职责是定位视图模板的位置。IView
接口;职责是从当前的上下文去合并数据与模板并转换为输出的Html标记。Q27. 介绍下Razor视图引擎?
Ans. Razor引擎是从MVC3引入的一种高级视图引擎。Razor不是一种新的语言而是一种新的标记语义。 Razor提供的语义减少用户输入且富于表现力。相较于Web From 语义更加简洁且容易学习。Razor使用@符号去书写标记。@Html.ActionLink("SignUp","SignUp")
Q28. 如何自定义视图引擎? Ans. ASP.NET MVC 是一个开源并且高度可扩展的框架。可以通过实现IViewENGIne 接口或者继承 VirtualPathProviderViewENGIne 抽象类来实现自定义视图引擎。
public class CustomViewENGIne : VirtualPathProviderViewENGIne
{
public CustomViewENGIne()
{
// Define the LOCATIOn of the View and Partial View
this.ViewLOCATIOnFormats = new String[] { "~/Views/{1}/{0}.html","~/Views/Shared/{0}.html" };
this.PartialViewLOCATIOnFormats = new String[] { "~/Views/{1}/{0}.html","~/Views/Shared/{0}.html" };
}
protected override IView CreatePartialView(ControllerContext
controllerContext,String partialPath)
{
var physicalpath =
controllerContext.httpContext.Server.MapPath(partialPath);
return new CustomView(physicalpath);
}
protected override IView CreateView(ControllerContext controllerContext,String viewPath,String masterPath)
{
var physicalpath =
controllerContext.httpContext.Server.MapPath(viewPath);
return new CustomView(physicalpath);
}
}
public class CustomView : IView
{
private String _viewPhysicalPath;
public CustomView(String ViewPhysicalPath)
{
_viewPhysicalPath = ViewPhysicalPath;
}
public void Render(ViewContext viewContext,System.IO.TextWriter writer)
{
//Load File
String rawcontents = File.ReadAllText(_viewPhysicalPath);
//Perform replacements
String parsedcontents = Parse(rawcontents,viewContext.ViewData);
writer.Write(parsedcontents);
}
public String Parse(String contents,ViewDataDictionary viewdata)
{
return Regex.replace(contents,"\\{(.+)\\}",m => GetMatch(m,viewdata));
}
public virtual String GetMatch(Match m,ViewDataDictionary viewdata)
{
if (m.success)
{
String key = m.Result("$1");
if (viewdata.ContainsKey(key))
{
return viewdata[key].ToString();
}
}
return String.Empty;
}
}
Q29. 如何注册自定义视图引擎?
Ans. 通过在global.asax.cs文件的Application_Start()
方法注册自定义视图引擎,来告诉ASP.NET MVC来使用自定义视图引擎替换默认的视图引擎。
protected void Application_Start()
{
//Register Custom View ENGIne
ViewENGInes.ENGInes.Add(new CustomViewENGIne());
//other code is removed for clarity
}
Q30. 能否删除默认的视图引擎? Ans. 当然可以删除微软提供达到默认搜索引擎,Razor和Web Froms(aspX)。
protected void Application_Start()
{
//Remove All View ENGIne including Webform and Razor
ViewENGInes.ENGInes.Clear();
}
Q31. Razor和WebForm 引擎的主要区别有哪些?
674_66@ |
---|
Q33. 列举下都有哪几种HTML Helpers? Ans. 主要有三种Html Helpers: 1、** Inline Html Helpers **- 通过Razor@Helper标签创建的帮助类,只能在同一个view中使用。
@Helper LisTingItems(String[] items)
{
}
ProgrAMMing Languages:
@LisTingItems(new String[] { "C","C++","C#" })
Book List:
@LisTingItems(new String[] { "How to C","how to C++","how to C#" })
2、 Built-In Html Helpers - Built-In Html Helpers 是针对HtmlHelper
的扩展方法。主要分为三类:
2.1 Standard Html Helpers - 用于渲染常见的Html 元素:s.jianshu.io/upload_images/2799767-b44505d018326fdb.png?imageMogr2/auto-orient/Strip%7CimageView2/2/w/1240">
2.2 Strongly Typed HTML Helpers - 基于model属性创建的html元素,主要是通过lambda表达式来创建元素。s.jianshu.io/upload_images/2799767-ee1c3348eabf68ea.png?imageMogr2/auto-orient/Strip%7CimageView2/2/w/1240">
2.3 Templated HTML Helpers - 该类型帮助类,会自动根据model类的属性类型去呈现适当的html元素。
比如你的属性使用[DataType(DataType.password)]属性注解为password,那么使用模板html帮助类,它将自动呈现为密码类型的文本框。password" name="password" type="password"/>
s.jianshu.io/upload_images/2799767-63ad1562fa0e50e4.png?imageMogr2/auto-orient/Strip%7CimageView2/2/w/1240">
Display and DisplayFor:根据指定的model属性和基于model属性的数据类型和元数据选择一个合适的html标签去渲染【只读状态的视图】 。Editor and EditorFor:根据指定的model属性和基于model属性的数据类型和元数据选择一个合适的html标签去渲染【编辑状态的视图】。
3、 Custom Html Helpers - 可以通过扩展HtmlHelper
类或者通过创建在工具类中创建静态方法来创建自定义Html Helper。
public static class CustomHelpers
{
//Submit Button Helper
public static MvcHtmlString SubmitButton(this HtmlHelper Helper,String buttontext)
{
String str = "ntext + "\"/>";
return new MvcHtmlString(str);
}
//Readonly Strongly-Typed TextBox Helper
public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper,Expression>expression,bool isReadonly)
{
MvcHtmlString html = default(MvcHtmlString);
if (isReadonly)
{
html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper,expression,new { @class = "readOnly",@readonly = "read-only" });
}
else
{
html = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper,expression);
}
return html;
}
}
Q34. 介绍下Url Helpers?
Ans. Url Helpers 基于路由配置帮助我们去渲染HTML链接或生成URL。s.jianshu.io/upload_images/2799767-0409df55f6f05d63.png?imageMogr2/auto-orient/Strip%7CimageView2/2/w/1240">
Q35. 什么是 Validation SumMary(验证汇总)?
Ans. ValidationSumMary是用来显示ModelState字典中所有验证错误信息 (未经排序)。它接收一个bool参数,当@Html.ValidationSumMary(true)
显示model-level错误;当 @Html.ValidationSumMary(false)
显示model-level 和 property-level 错误。
假设你在代码中添加了如下两行代码:
@H_114_20@modelState.AddModelError("","This is Model-level error!"); ModelState.AddModelError("Name","This Model property-level error!");
第一行未指定key关联这个错误信息;第二行指定Name属性关联。 当在view中分别使用以下两种方式现在验证信息:
@Html.ValidationSumMary(true) @*//shows model-level errors*@
@Html.ValidationSumMary(false) @*//shows model-level and property-level errors*@
因此,为true时,只显示model级别错误信息。 为false时,即显示model级别,也显示model的属性级别的错误。
Q36. 介绍下 AJAX Helpers?
Ans. AJAX Helpers 是用来创建启用Ajax进行异步加载的元素比如Ajax From,Ajax 链接。 AJAX Helpers 是System.Web.Mvc
命名空间中AJAXHelper
类的扩展方法。
@Ajax.ActionLink("Load Products","GetProducts",new AjaxOptions {updateTargetId= "Products-container",httpR_838_11845@ethod = "GET" })
//输出:
<a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace"
data-ajax-update="#Products-container" href="/Home/GetProducts">Load Products
Q37.什么是非侵入Ajax (unobtrusive AJAX)? Ans. ASP.NET MVC提供了基于jquery的非侵入ajax。非侵入式ajax意味着通过使用帮助类方法去定义ajax功能而不是通过在view中添加js代码块。
Q38. 介绍下AJAX帮助类的配置选项?
Ans. AjaxOptions
类定义的属性允许你在ajax请求的生命周期中的不同阶段指定对应的回调方法。关于AjaxOptions
的配置项讲解,详参http://www.jianshu.com/p/4ccb941357b6">Ajax.beiginForm()知多少。
Q39. 什么是Cross Domain AJAX(跨域Ajax)? Ans. 默认来说,浏览器只允许ajax调用你自己服务器上托管的当前web应用的站点。这个限制帮助组长了许多安全问题(比如XSS攻击)。但是,有些时候我们需要与额外的API(s)交互比如(Twitter或者Google)。因此,为了与额外的API(s)进行交互,我们的web应用就必须支持JSONP请求或CORS(跨域资源分享)。ASP.NET MVC 默认不支持JSONP和CORS。如果有这种需要,就需要做一些编码和配置。
以上是大佬教程为你收集整理的Asp.net mvc 知多少(三)全部内容,希望文章能够帮你解决Asp.net mvc 知多少(三)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。