???Asp.net MVC ?????????
???????????? ???????[ 2016/3/25 9:44:37 ] ??????????????????? ???????
???????????????д???????View???????????д??View??????????????ThemeViewEngine?ж????·?????£???ServiceController??????????дocean??sky???????View??
????????web.config??????Theme????ocean??????μ?View????????????
?????塢Validator
?????????Model???????Attribute??????????MVC????????????????????????????????????????????????????????????????????????????ValidationAttribute???????System.ComponentModel.DataAnnotations???????
????????????????е?ValidationAttribute??????????????????????????????????????????Attribute??????????????????AgeValidator??
public class AgeValidator: ValidationAttribute
{
public AgeValidator()
{
ErrorMessage = "Please enter the age>18";
}
public override bool IsValid(object value)
{
if (value == null)
return false;
int age;
if (int.TryParse(value.ToString()?? out age))
{
if (age > 18)
return true;
return false;
}
return false;
}
}
??????????AgeValidator?????????MVC?????ValiatorAttribute???????
????[Required]
????[AgeValidator]
????public int? Age { get; set; }
????????????????????????????????????????????Model?ж???????????????ж?????????????????????????????????Model???IValidatableObject???????
????public class UserViewModel:IValidatableObject
????{
????public string Name { get; set; }
????[Required]
????[AgeValidator]
????public int? Age { get; set; }
????public IEnumerable Validate(ValidationContext validationContext)
????{
????if(string.IsNullOrEmpty(Name))
????yield return new ValidationResult("the name can not be empty");
????if (Name.Equals("lucy"))
????{
????if(Age.Value<25)
????yield return new ValidationResult("lucy's age must greater than 25");
????}
????}
????}
????????ModelBinder
????Model??????????????????????????????????Action??????????С?
????public ActionResult InputAge(UserViewModel user)
????{
????//...
????return View();
????}
?????????????????Action???????Post????MVC?????Form?е???????user?????У??????get????MVC?????QueryString????????user?????С?
??????????????????????????????????POST???XML????????????????MVC????????????????????????????????????????????????Action??????????С??????????????????XmlModelBinder??
public class XmlModelBinder:IModelBinder
{
public object BindModel(ControllerContext controllerContext?? ModelBindingContext bindingContext)
{
try
{
var modelType = bindingContext.ModelType;
var serializer = new XmlSerializer(modelType);
var inputStream = controllerContext.HttpContext.Request.InputStream;
return serializer.Deserialize(inputStream);
}
catch
{
bindingContext.ModelState.AddModelError(""?? "The item could not be serialized");
return null;
}
}
}
???????????????????ModelBinder??????????????????Attribute???????????ModelBinder:
????public ActionResult PostXmlContent([ModelBinder(typeof(XmlModelBinder))]UserViewModel user)
????{
????return new XmlResult(user);
????}
???????????PostMan??????????????
??????????????????MVC???Action???????????XmlModelBinder???????????????????XmlModelBinderProvider?????????MVC?????????????????XmlModelBinder??
????public class XmlModelBinderProvider: IModelBinderProvider
????{
????public IModelBinder GetBinder(Type modelType)
????{
????var contentType = HttpContext.Current.Request.ContentType.ToLower();
????if (contentType != "text/xml")
????{
????return null;
????}
????return new XmlModelBinder();
????}
????}
???????Provider??????MVC??????????????text/xml?????????XmlModelBinder??
????public class MvcApplication : System.Web.HttpApplication
????{
????protected void Application_Start()
????{
????ModelBinderProviders.BinderProviders.Insert(0?? new XmlModelBinderProvider());
????//...
????}
????????XmlModelBinderProvier??????????????????Action?е?????????ú???ModelBinder:
????public ActionResult PostXmlContent(UserViewModel user)
????{
????return new XmlResult(user);
????}
????????????ControllerFactory??????????
????MVC????DefaultControllerFactory??????????????Controller????????????Action???????????????????????????????ControllerFactory??????IOC??????????Controller?????
??????Castle????????????WindsorControllerFactory???????????ContainerInstaller????????齨??????????У??????ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));??MVC??ControllerFactory??????????????WindsorControllerFactory??
????????????????Nuget??????????????????????????
????Install-Package Castle.Windsor.Web.Mvc
??????????????趼?????????????????????????
????public class ProvidersInstaller:IWindsorInstaller
????{
????public void Install(IWindsorContainer container?? IConfigurationStore store)
????{
????container.Register(Component.For().ImplementedBy().LifestylePerWebRequest());
????}
????}
????Controller??????й???????????
????private readonly IUserProvider _userProvider;
????public ServiceController(IUserProvider userProvider)
????{
????_userProvider = userProvider;
????}
????public ActionResult GetUserByIoc()
????{
????var user = _userProvider.GetUser();
????return new XmlResult(user);
????}
??????????Lambda Expression Tree???MVC????
???????????????MVC???????????????????Lambda Expression Treeд??????????????????ActionLink????????????
????public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper?? string linkText?? string actionName?? object routeValues?? object htmlAttributes);
??????Razor??棬???@Html.ActionLink(“Line item 1″?? “OrderLineItem”?? “Service”?? new { id = 1 })????????a????????????????????Controller??Action?????????????????????????????????????????в??????????????Controller??Action???????д???????????????????
???????????????Lambda Expression Tree??????Controller??Action??????????????????????дController??Action??????????????????????????????????????????ο?Expression Tree ???MVC?е? HtmlHelper ?? UrlHelper????????????????????????
<div class="row">
<h2>Mvc way</h2>
<ul>
<li>@Html.ActionLink("Line item 1"?? "OrderLineItem"?? "Service"?? new { id = 1 }) </li>
<li>@Html.ActionLink("Line item 2"?? "OrderLineItem"?? "Service"?? new { id = 2 })</li>
<li>@Url.Action("OrderLineItem"??"Service"??new {id=1})</li>
<li>@Url.Action("OrderLineItem"??"Service"??new {id=2})</li>
</ul>
</div>
<div class="row">
<h2>Lambda Expression tree</h2>
<ul>
<li>@Html.ActionLink("Line item 1"?? (ServiceController c) => c.OrderLineItem(1))</li>
<li>@Html.ActionLink("Line item 2"?? (ServiceController c) => c.OrderLineItem(2))</li>
<li>@Url.Action((ServiceController c)=>c.OrderLineItem(1))</li>
<li>@Url.Action((ServiceController c)=>c.OrderLineItem(2))</li>
</ul>
</div>
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11