自定义模型绑定的ASP.NET MVC的GET请求 [英] Custom Model Binder for ASP.NET MVC on GET request
问题描述
我创建了一个自定义的MVC模型绑定都会调用每 HttpPost
是进入服务器。但不会被调用为 HTTPGET
请求。
I've created a custom MVC Model Binder which gets called for every HttpPost
that comes into the server. But does not get called for HttpGet
requests.
- 如若我自定义模型绑定期间被称为
GET
?如果是这样,我错过了什么? - 如果不是,我怎么可以编写自定义代码处理
查询字符串
从GET
请求?
- Should my custom model binder get called during a
GET
? If so, what did I miss? - If not, How can I write custom code handling the
QueryString
from aGET
Request?
下面是我的实现。 ..
Here's my implementation...
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中
Global.asax
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
//...
}
我看着这些解决方案,但他们不是那么回事了什么,我在寻找:
I've looked into these solutions, but they don't quite work for what I'm looking for:
- 通过坚持复杂类型
的TempData
- 使用默认联建立复杂类型(
名称=约翰&放大器;?姓= Doe的
)
- Persisting complex types via
TempData
- Using the default binder to build up complex types (
?Name=John&Surname=Doe
)
感谢@Felipe的帮助。万一有人用同样的斗争,我了解到:
Thanks to @Felipe for the help. Just in case someone struggles with the same, I learnt:
- 在自定义模型粘合剂的可以用于
GET
要求 - 您的 CAN 使用
DefaultModelBinder
类 - 我的障碍是,在操作方法必须有一个参数(否则模型粘合剂跳过
GET
的要求,这使得当你想想它的意义)
- The custom model binder CAN be used for
GET
requests - You CAN use
DefaultModelBinder
class - My snag was that the action method MUST have a parameter (otherwise the model binder is skipped for
GET
Requests, which makes sense when you think about it)
推荐答案
让我们supose你有你自己的类型要绑定。
Let's supose you have your own type you want to bind.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
// other properties you need
}
您可以创建自定义这种特定类型的模型绑定,从 DefaultModelBinder
inherithing,样品:
You can create a custom model bind for this specific type, inherithing from DefaultModelBinder
, for sample:
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 };
}
}
在Global.asax中的的Application_Start
事件,可以注册表这个模型绑定,为示例:
In the Global.asax in the Application_Start
event, you can registry this model bind, for sample:
// for Person type, bind with the PersonModelBinder
ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder());
在从<$的 BindModel
方法C $ C> PersonModelBinder ,请确保您有查询字符串的所有参数,并给他们理想的治疗方法。
In the BindModel
method from the PersonModelBinder
, make sure you have all parameters in the querystring and give them the ideal treatment.
既然你有这样的操作方法
Since you have this action method:
public ActionResult Test(Person person)
{
// process...
}
您可以用URL像这样访问这个动作:
You can access this action with an url something like this:
Test?id=7&name=Niels&age=25
这篇关于自定义模型绑定的ASP.NET MVC的GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!