Asp.net mvc 2 in action 笔记-3 Areas AJAX AutoMapper
Areas
第21、22章
分组管理Controller
关键点
在Global.asax.cs增加AreaRegistration.RegisterAllAreas();
重载AreaRegistration 的RegisterArea函数,定义路由
其他都和普通的Controller一样,每个Area下的目录也包含MVC目录
方便移植的Area:程序逻辑和视图等打包 在一个库文件中,其他我web引用即可使用,提供了例子实现。
AJAX
第12、27章
MVC项目默认集成了JQuery库和Microsoft AJAX库(最初的Atlas)
AJAX Helper
System.Web.Mvc ..::. AjaxHelper
页面必须包含脚本
<script src="http://www.cnblogs.com/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<ul id="comments">
</ul>
<% using(Ajax.BeginForm("AddComment", new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "comments",
InsertionMode = InsertionMode.InsertAfter
})) { %>
<%= Html.TextArea("Comment", new{rows=5, cols=50}) %>
<button type="submit">Add Comment</button>
<% } %>
<h3>Ajax.Link</h3>
<%= Ajax.ActionLink("Show the privacy Policy", "PrivacyPolicy",
new AjaxOptions{InsertionMode = InsertionMode.Replace, UpdateTargetId = "privacy"}) %>
<div id="privacy"></div>
可见以上的封装简化了AJAX的处理
控制器的处理可以使用
ContentResult JsonResult等返回结果
JQuery
这个图书 Juery In Action 描述的十分详细
AutoMapper
第18章
应用场景:实现Domain Model向Presentation Model的转换[例如Domain Model的对象关系较多等等情况],比如数据类型的转换和格式化等等都可以使用这个处理,以减少View处理的复杂性
初始化
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x => x.AddProfile<ExampleProfile>());
}
Global.asax.cs:
protected void Application_Start()
{
AutoMapperConfiguration.Configure();
AutoMapper profiles
a profile is a collection of type-mapping definitions, including rules that apply to all maps defined in the profile.
public class ExampleProfile : Profile
{
protected override string ProfileName
{
get { return "ViewModel"; }
}
protected override void Configure()
{
AddFormatter<HtmlEncoderFormatter>();
ForSourceType<Name>().AddFormatter<NameFormatter>();
ForSourceType<decimal>()
.AddFormatExpression(context =>
((decimal)context.SourceValue).ToString("c"));
CreateMap<Customer, CustomerInfo>()
.ForMember(x => x.ShippingAddress, opt =>
{
opt.AddFormatter<AddressFormatter>();
opt.SkipFormatter<HtmlEncoderFormatter>();
});
CreateMap<Customer, CustomerInput>().AfterMap((c, ci) => CreateSelectList(c, ci));
}
private static void CreateSelectList(Customer customer, CustomerInput input)
{
IEnumerable<CustomerStatus> allCustomerStatuses = Enumeration.GetAll<CustomerStatus>();
int selectedValue = customer.Status.Value;
input.AllStatuses = new SelectList(allCustomerStatuses, "Value", "DisplayName", selectedValue);
}
}
应用
定义一个Action Filter
public class AutoMapModelAttribute : ActionFilterAttribute
{
private readonly Type _destType;
private readonly Type _sourceType;
public AutoMapModelAttribute(Type sourceType, Type destType)
{
_sourceType = sourceType;
_destType = destType;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
object model = filterContext.Controller.ViewData.Model;
object viewModel = Mapper.Map(model, _sourceType, _destType);
filterContext.Controller.ViewData.Model = viewModel;
}
}
在具体的Action上述性标注即可实现转换
[AutoMapModel(typeof(IEnumerable<Customer>), typeof(IEnumerable<CustomerInfo>))]
视图可以使用转换后的类型,如上例的CustomerInfo
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南