Asp.net Razor网页将值绑定到对象
我只是想知道是否有一种方法可以像MVC中的网页一样将即将到来的请求值绑定到一个对象 与mvc相比,Web页面有一种更简单的方法来开发更小的网站。所以我现在想用razor网页来开发网站 我使用MVC很长一段时间,并且有基于约定的自动模型绑定。在任何操作中,您都可以使用Asp.net Razor网页将值绑定到对象,razor,asp.net-webpages,Razor,Asp.net Webpages,我只是想知道是否有一种方法可以像MVC中的网页一样将即将到来的请求值绑定到一个对象 与mvc相比,Web页面有一种更简单的方法来开发更小的网站。所以我现在想用razor网页来开发网站 我使用MVC很长一段时间,并且有基于约定的自动模型绑定。在任何操作中,您都可以使用TryUpdateModel()方法在复杂对象上绑定请求值,以获得额外的工作 在第页中可以看到一个ModelBinders.Binders.DefaultBinder.BindModel()方法,但它需要两个长参数 我想知道是否有一种
TryUpdateModel()
方法在复杂对象上绑定请求值,以获得额外的工作
在第页中可以看到一个ModelBinders.Binders.DefaultBinder.BindModel()
方法,但它需要两个长参数
我想知道是否有一种简单快速的方法可以将请求参数绑定到C#object:)
Thx以获取帮助。网页框架中没有模型绑定。该框架主要是由新手开发人员开发的,从我当时看到的项目团队成员的评论来看,他们认为他们的目标受众不会理解或使用数据(业务对象)的强类型容器 我的一个项目需要这样的东西,所以我建立了自己的非常简单的模型活页夹。这是一个粗糙的代码,只处理简单的类型,没有经过任何修改,但它符合我的目的。您可以将其用于更强大的基础:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
public static class RequestBinder
{
/// <summary>
/// Generates entity instances from Request values
/// </summary>
/// <typeparam name="TEntity">The Type to be generated</typeparam>
/// <param name="request"></param>
/// <returns>TEntity</returns>
public static TEntity Bind<TEntity>(this HttpRequestBase request) where TEntity : class, new()
{
var entity = (TEntity)Activator.CreateInstance(typeof(TEntity));
var properties = typeof(TEntity).GetProperties();
foreach (var property in properties)
{
object safeValue;
Type t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
try
{
if (t == typeof(String))
{
safeValue = string.IsNullOrEmpty(request[property.Name]) ? null : request[property.Name];
}
else if (t.IsEnum)
{
safeValue = (request[property.Name] == null) ? null : Enum.Parse(t, request[property.Name]);
}
else
{
Type tColl = typeof(ICollection<>);
if (t.IsGenericType && tColl.IsAssignableFrom(t.GetGenericTypeDefinition()) || t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == tColl))
{
continue;
}
safeValue = (request[property.Name] == null) ? null : Convert.ChangeType(request[property.Name], t);
}
property.SetValue(entity, safeValue, null);
}
catch (Exception)
{
}
}
return entity;
}
/// <summary>
/// Populates an existing entity's properties from the Request.Form collection
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="request"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static TEntity Bind<TEntity>(this HttpRequestBase request, TEntity entity) where TEntity : class, new()
{
foreach (string item in request.Form)
{
var property = entity.GetType().GetProperty(item);
if (property != null)
{
object safeValue;
Type t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
try
{
if (t == typeof(String))
{
safeValue = string.IsNullOrEmpty(request[property.Name]) ? null : request[property.Name];
}
else if (t.IsEnum)
{
safeValue = (request[property.Name] == null) ? null : Enum.Parse(t, request[property.Name]);
}
else
{
Type tColl = typeof(ICollection<>);
if (t.IsGenericType && tColl.IsAssignableFrom(t.GetGenericTypeDefinition()) || t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == tColl))
{
continue;
}
safeValue = (request[property.Name] == null) ? null : Convert.ChangeType(request[property.Name], t);
}
property.SetValue(entity, safeValue, null);
}
catch (Exception)
{
}
}
}
return entity;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
运用系统反思;
使用System.Web;
公共静态类请求绑定器
{
///
///从请求值生成实体实例
///
///要生成的类型
///
///张力
公共静态TEntity绑定(此HttpRequestBase请求),其中TEntity:class,new()
{
var entity=(tenty)Activator.CreateInstance(typeof(tenty));
var properties=typeof(tenty).GetProperties();
foreach(属性中的var属性)
{
客体安全价值;
类型t=Nullable.GetUnderlineType(property.PropertyType)??property.PropertyType;
尝试
{
if(t==typeof(String))
{
safeValue=string.IsNullOrEmpty(请求[property.Name])?null:request[property.Name];
}
else if(t.IsEnum)
{
safeValue=(请求[property.Name]==null)?null:Enum.Parse(t,请求[property.Name]);
}
其他的
{
类型tColl=类型of(ICollection);
if(t.IsGenericType&&tColl.IsAssignableFrom(t.GetGenericTypeDefinition())| | t.GetInterfaces().Any(x=>x.IsGenericType&&x.GetGenericTypeDefinition()==tColl))
{
继续;
}
safeValue=(请求[property.Name]==null)?null:Convert.ChangeType(请求[property.Name],t);
}
SetValue(实体,安全值,空);
}
捕获(例外)
{
}
}
返回实体;
}
///
///从Request.Form集合填充现有实体的属性
///
///
///
///
///
公共静态TEntity绑定(此HttpRequestBase请求,TEntity实体),其中TEntity:class,new()
{
foreach(request.Form中的字符串项)
{
var property=entity.GetType().GetProperty(项);
if(属性!=null)
{
客体安全价值;
类型t=Nullable.GetUnderlineType(property.PropertyType)??property.PropertyType;
尝试
{
if(t==typeof(String))
{
safeValue=string.IsNullOrEmpty(请求[property.Name])?null:request[property.Name];
}
else if(t.IsEnum)
{
safeValue=(请求[property.Name]==null)?null:Enum.Parse(t,请求[property.Name]);
}
其他的
{
类型tColl=类型of(ICollection);
if(t.IsGenericType&&tColl.IsAssignableFrom(t.GetGenericTypeDefinition())| | t.GetInterfaces().Any(x=>x.IsGenericType&&x.GetGenericTypeDefinition()==tColl))
{
继续;
}
safeValue=(请求[property.Name]==null)?null:Convert.ChangeType(请求[property.Name],t);
}
SetValue(实体,安全值,空);
}
捕获(例外)
{
}
}
}
返回实体;
}
}
您可以这样使用它:
var person = Request.Bind<Person>();
var person=Request.Bind();
目前还不清楚网页将如何整合到ASP.NET vNext中。ASP.NET团队讨论了消除网页、MVC和Web API之间的重复,因此希望这将导致网页包括模型绑定