asp.net – C#Web API模型绑定提供程序应如何工作?
|
我有以下内容: >请求网址:’端点/ 1,2,3?q = foo’ 我想将“1,3”片段映射到“ids”参数,因此我根据this link创建了一个ModelBinderProvider,它应该调用正确的模型绑定器. public class MyModelBinderProvider: ModelBinderProvider
{
public override IModelBinder GetBinder(HttpConfiguration configuration,Type modelType)
{
IModelBinder modelBinder = null;
if (modelType.IsGenericType && (modelType.GetGenericTypeDefinition() == typeof(List<>)))
{
modelBinder = new ListModelBinder();
}
return modelBinder;
}
}
我在Global.asax中注册了提供者,如下所示: GlobalConfiguration.Configuration.Services.Insert(typeof(ModelBinderProvider),new MyModelBinderProvider()); 原因是:我创建了这个提供程序,因为我想要,无论T是什么(‘1,3’或’一,二,三’),绑定才能工作. 问题: 奇怪的是:做这样的事情有效,但T是专门的,因此不是我想要的: var simpleProvider = new SimpleModelBinderProvider(typeof(List<int>),new ListModelBinder()); GlobalConfiguration.Configuration.Services.Insert(typeof(ModelBinderProvider),simpleProvider); 我看不出我做错了什么,为什么’modelType’参数不是预期值? 解决方法这是一个非常古老的问题,但我在遗留代码中遇到了类似的问题.逗号是保留的,应该避免,虽然它们在某些情况下工作,但如果你真的想使用它们…… 一旦“1,3”是网址的路径部分,我认为这更像是路径问题而不是模型绑定器.假设这个我编写了一个小的RouteHandler来完成这个技巧(请原谅非常简单的“单词到整数”翻译器). CsvRouteHandler从URL获取id数组,并将其作为整数数组放在RouteData上.如果原始数组包含一个,两个或三个单词,则会将每个值转换为int. MvcRouteHandler protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
{
var idArrayParameter = requestContext.RouteData.Values["idArray"] != null ? requestContext.RouteData.Values["idArray"].ToString() : null;
if (string.IsNullOrEmpty(idArrayParameter))
{
return base.GetHttpHandler(requestContext);
}
requestContext.RouteData.Values.Remove("idArray"); // remove the old array from routedata
// Note: it is horrible and bugged but and you probably have your own translation method :)
string[] idArray = idArrayParameter.Split(',');
int[] ids = new int[idArray.Length];
for(int i = 0; i < idArray.Length; i++)
{
if (!int.TryParse(idArray[i],out ids[i]))
{
switch (idArray[i])
{
case "one":
ids[i] = 1;
break;
case "two":
ids[i] = 2;
break;
case "three":
ids[i] = 3;
break;
}
}
}
requestContext.RouteData.Values.Add("Id",ids);
return base.GetHttpHandler(requestContext);
}
}
路线配置: routes.Add(
name: "Id Array Route",item: new Route(
url: "endpoint/{idArray}",defaults: new RouteValueDictionary(new { controller = "Test",action = "Index" }),routeHandler: new CsvRouteHandler())
); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 如何在aspx页面中隐藏ASP.NET自定义控件的属性?
- asp.net-mvc – 用于枚举的IRouteConstraint
- asp-classic – 经典ASP的好IDE?
- 如何在ASP.NET应用程序中避免SQL注入攻击?
- asp.net-mvc – 在MVC3 Razor视图引擎中设置文本框的可见性
- asp.net – 有人有一种方法来保持页面呈现一旦一个人已退出
- asp.net – Nlog不创建相对于网站项目的文件
- asp.net-mvc – 如何在ASP.NET MVC中阻止JSON序列化?
- asp.net-mvc – 我们是否正在使用.Net 3.5中的MVC框架转向经
- asp.net-mvc-3 – StringLength属性行为
- asp.net-mvc – 将ViewData传递给RenderPartial
- asp.net – WebResource.axd空白或找不到
- asp.net – 选择下拉列表项目findbytext没有区分
- asp.net – XMLHTTP请求的经典ASP错误
- asp.net-mvc – ASP.NET MVC中的代码
- asp.net-mvc-3 – ASP.Net MVC 3,Ninject和Quart
- asp.net – 在后面的代码中无法识别嵌套的Repeat
- asp.net – MiniProfiler.Stop()上的MVC Mini Pr
- asp.net-mvc – ASP.NET MVC ActionFilter参数绑
- asp.net-mvc – 在asp.net mvc控制器中使用构造函
