如何禁用ASP.NET MVC的路由值的绑定? [英] How to disable binding of the route values in ASP.NET MVC?

查看:81
本文介绍了如何禁用ASP.NET MVC的路由值的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知ValueProviderDictionary取值从3个地方结合


  1. 从帖子的形式

  2. 从路由值

  3. 从查询字符串

我希望能够禁用第二和第三选项。我在这里看到的唯一的解决方案是创建自己的自定义ValueProvider(复制 - 粘贴ValueProviderDictionary源文件,并从那里删除这两个选项)。

这是正确的解决方案?是否有更优雅的替代品?

(如果使用自定义ValueProvider解决方案是正确的,它应该被初始化?在控制器的操作方法或自定义ModelBinder的?)

感谢您


解决方案

我做了Action属性和ValueProvider解决这一问题。把它命名为GetValues​​Attribute和StrictValueProviderDictionary RESP。您可以设置源形式的RouteData或查询字符串。例如,如果你想要一个ValueProvider将搜索表格值,也许查询字符串写你的方法或控制器上面的以下内容:

  [的GetValues​​(ValueSource.Form | ValueSource.QueryString)
的[AcceptVerbs(HttpVerbs.Post)
公众的ActionResult创建(的FormCollection formValues​​)
{
   ...
   的UpdateModel(基于myModel); //模式将从提到来源更新

源$ C ​​$ C被放在这里:的http://$c$cpaste.net/2kpzct 。唯一的问题是,它不会工作,如果你想获得填充模式作为参数。<​​/ P>

As I know ValueProviderDictionary takes values for binding from 3 places

  1. From Post form
  2. From Route values
  3. From Query string

I want to be able to disable the second and the third options. The only solution I see here is to create my own custom ValueProvider (copy-paste ValueProviderDictionary source file and remove these two options from there).

Is this solution correct? Are there any more elegant alternatives?

(If the solution with a custom ValueProvider is correct where it should be initialized? In a Controller's action method or in a custom ModelBinder?)

Thank you

解决方案

I've made Action attribute and ValueProvider for solving this problem. Named it GetValuesAttribute and StrictValueProviderDictionary resp. You can set the source Form, RouteData or QueryString. For example if you want that ValueProvider would search for values in Form and maybe QueryString write the following above your method or controller:

[GetValues(ValueSource.Form | ValueSource.QueryString)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formValues)
{
   ...
   UpdateModel(myModel); //model will be updated from mentioned sources

The source code is placed here: http://codepaste.net/2kpzct. The only problem is that it won't work if you'll want to get populated model as a parameter.

这篇关于如何禁用ASP.NET MVC的路由值的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆