C# 根据GET请求为ASP.NET MVC定制模型绑定器
我已经创建了一个定制的MVC模型绑定器,它会被服务器中的每个C# 根据GET请求为ASP.NET MVC定制模型绑定器,c#,asp.net-mvc,http-get,custom-model-binder,C#,Asp.net Mvc,Http Get,Custom Model Binder,我已经创建了一个定制的MVC模型绑定器,它会被服务器中的每个HttpPost调用。但不会为HttpGet请求调用 在get过程中是否应该调用我的自定义模型绑定器?如果是,我错过了什么? 如果没有,如何编写自定义代码来处理GET请求中的QueryString 这是我的实现 public class CustomModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext cont
HttpPost
调用。但不会为HttpGet
请求调用
- 在
过程中是否应该调用我的自定义模型绑定器?如果是,我错过了什么?get
- 如果没有,如何编写自定义代码来处理
请求中的GET
QueryString
public class CustomModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// This only gets called for POST requests. But I need this code for GET requests.
}
}
Global.asax
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
//...
}
我已经研究了这些解决方案,但它们并不完全适合我所寻找的:
- 通过
TempData
- 使用默认绑定器构建复杂类型(
)?Name=John&Name=Doe
- 自定义模型活页夹可用于
请求GET
- 您可以使用
类DefaultModelBinder
- 我的问题是操作方法必须有一个参数(否则,
请求将跳过模型绑定器,这在您考虑时是有意义的)GET
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
// other properties you need
}
您可以为此特定类型创建自定义模型绑定,该绑定继承自DefaultModelBinder
,用于示例:
public class PersonModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.HttpContext.Request;
int id = Convert.ToInt32(request.QueryString["id"]);
string name = request.QueryString["name"];
int age = Convert.ToInt32(request.QueryString["age"]);
// other properties
return new Person { Id = id, Name = name, Age = age };
}
}
// for Person type, bind with the PersonModelBinder
ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder());
在应用程序_Start
事件的Global.asax中,您可以注册此模型绑定,例如:
public class PersonModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.HttpContext.Request;
int id = Convert.ToInt32(request.QueryString["id"]);
string name = request.QueryString["name"];
int age = Convert.ToInt32(request.QueryString["age"]);
// other properties
return new Person { Id = id, Name = name, Age = age };
}
}
// for Person type, bind with the PersonModelBinder
ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder());
在PersonModelBinder
中的BindModel
方法中,确保查询字符串中包含所有参数,并对它们进行理想的处理
由于您有此操作方法:
public ActionResult Test(Person person)
{
// process...
}
您可以使用如下url访问此操作:
Test?id=7&name=Niels&age=25
您可以使用筛选每个请求,在该方法中,您可以使用
请求
@JSantosh获取查询字符串,假设我这样做了,并在操作执行
中获得了我的查询字符串
。我无法将QueryString转换为对象并将其作为参数传递给action方法,对吗?这让我陷入了同样的困境。在post请求中,您将数据作为对象,并自动解析到相应的模型中,但在get中,您将数据作为字符串,我不确定您是否能够实现这一点。好问题:)谢谢@JSantosh:)我的代码用于将查询字符串从字符串转换为自定义对象,这很好。现在只需将构建的对象设置为动作参数……感谢您的详细回答。我使用的是DefaultModelBinder
(请参阅我的问题),在get请求期间,它似乎没有被点击,只是用于POST。您的BindModel
方法是否也被调用为HttpGet
方法?我已经在这里进行了测试,并且在Test
action方法中,我得到了person
参数填充ok。确保已为自定义类型注册了自定义绑定。请记住,您还需要阅读request.QueryString
。太棒了,那么我错过了一些东西。让我来玩,然后再打给你。谢谢,谢谢,菲利佩,现在开始工作了。事实证明,如果使用不带任何参数的doGet
request toAction
方法,它不会通过模型绑定器(我猜为什么需要它)@Niels Filter感谢您的观点“如果使用不带任何参数的do-Get request to Action方法,它不会通过模型绑定器”。该参数似乎是触发MVC模型绑定所必需的,即使它是一个伪参数。