MVC自定义模型绑定的GET请求 [英] MVC Custom Model Binder for 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
?如果是这样,我错过了什么? - 如果不是,我怎么可以编写自定义code处理
查询字符串
从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?
下面是我执行...
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 code>坚持复杂类型
- 使用默认联建立复杂类型(?
名称=约翰&安培;姓= 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:
- 在自定义模型粘合剂的 CAN 用于
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
从法 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
这篇关于MVC自定义模型绑定的GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!