如何在ASP.Net MVC路由段映射1或0为布尔操作方法输入参数 [英] How to map a 1 or 0 in an ASP.Net MVC route segment into a Boolean action method input parameter

查看:253
本文介绍了如何在ASP.Net MVC路由段映射1或0为布尔操作方法输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些调入一些ASP.NET MVC端点PHP和JavaScript的应用程序。比方说,我们有这个端点:

We have some PHP and Javascript apps that call into some ASP.NET MVC endpoints. Let's say we have this endpoint:

public ActionResult DoSomething(bool flag)
{

}

我想它是否我传递的1或0的整数。匹配标志的值,或传递真或假的字符串。哪一部分的框架,我需要为了实现以匹配呢?

I want it to match the value for flag whether I pass in an integer of 1 or 0, or pass in a string of "true" or "false". What part of the framework do I need to implement in order to match that up?

推荐答案

要做到这一点,最好的方法是使用自定义值提供商。虽然你可以做到这一点使用一个完整的定制模型绑定,即矫枉过正根据您的要求,而且它是非常容易简单地实现自定义值提供商。

The best way to do this is using a custom value provider. While you can do this using a complete custom model binder, that is overkill based on your requirements, and it is much easier simply to implement a custom value provider.

有关当您使用自定义模型粘合剂以及何时使用自定义值提供一些指导,请参阅here和<一个href=\"http://www.fattybeagle.com/2011/09/21/using-a-custom-ivalueprovider-in-asp-net-mvc/\">here.

For some guidance on when you use a custom model binder and when to use a custom value provider, see here and here.

您可以只创建一个自定义值提供商来处理其旗帜的一个重要途径价值观和处理int类型的值提供者为bool的转换。在code这样做看起来是这样的:

You can just create a custom value provider to handle route values having a key of "flag" and handle the int to bool conversion in the value provider. The code to do this looks something like this:

public class IntToBoolValueProvider : IValueProvider
{
    public IntToBoolValueProvider(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");

        this._context = context;
    }
    public bool ContainsPrefix(string prefix)
    {
        return prefix.ToLower().IndexOf("flag") > -1;
    }
    public ValueProviderResult GetValue(string key)
    {
        if (ContainsPrefix(key))
        {
            int value = 0;
            int.TryParse(_context.RouteData.Values[key].ToString(), out value);
            bool result = value > 0;
            return new ValueProviderResult(result, result.ToString(), CultureInfo.InvariantCulture);
        }
        else
        {
            return null;
        }
    }
    ControllerContext _context;
}

public class IntToBoolValueProviderFactory : ValueProviderFactory
{
    public override IValueProvider GetValueProvider(ControllerContext controllerContext)
    {
        return new IntToBoolValueProvider(controllerContext);
    }
}

在价值提供者,你实现了包含preFIX方法的路线值键你有兴趣返回true,在这种情况下,关键的旗帜。在国旗的GetValue,你的旗帜路由数据项的值转换为int,然后一个布尔值,具体取决于该int是大于零。对于不属于旗帜的所有其它路由数据钥匙,你只是返回null,它告诉MVC框架忽略这个ValueProvider,并移动到其他的价值提供者。

In the value provider, you implement the ContainsPrefix method to return true for the route value keys you are interested in, in this case the key "flag". In the GetValue flag, you convert the value of the "flag" route data entry to an int, and then to a boolean, depending if the int is greater than zero. For all other route data keys that are not "flag", you just return null, which tells the MVC framework to ignore this ValueProvider and move on to other value providers.

要这样使用,需要实现ValueProviderFactory的子类,创建自定义的IntToBoolValueProvider提供商。另外,你需要给该工厂的MVC框架注册。您可以使用静态ValueProviderFactories班在Global.asax中做到这一点:

To wire this up, you need to implement a subclass of ValueProviderFactory which creates the custom IntToBoolValueProvider provider. Also, you need to register this factory with the MVC framework. You do this in global.asax using the static ValueProviderFactories class:

protected void Application_Start()
{
    ValueProviderFactories.Factories.Insert(0, new IntToBoolValueProviderFactory());
}

如果你就必须建立一个路线如下:

If you then have a route set up as follows:

routes.MapRoute("", "{controller}/foo/{flag}", new { action = "Foo" });

这条路线将请求定向到

this route will direct requests to

http://localhost:60286/Home/Foo/{flag}

要操作方法

    public ActionResult Foo(bool flag)
    {
        //Implement action method
        return View("Index");
    }

当的{标志}段大于0时,布尔标志输入参数将是正确的,而当它是零,标志参数将是假的。

When the {flag} segment is greater than 0, the bool flag input parameter will be true, and when it is zero, the flag parameter will be false.

更多关于MVC自定义值提供者可以在这里找到

More on MVC custom value providers can be found here.

这篇关于如何在ASP.Net MVC路由段映射1或0为布尔操作方法输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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