处理MVC2变量,在他们的名字连字符。 [英] Handling MVC2 variables with hyphens in their name

查看:96
本文介绍了处理MVC2变量,在他们的名字连字符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与在其名称中的连字符创建查询字符串参数一些第三方软件的工作。我正在看这太问题,似乎像他们的解决方案是非常接近我所需要的,但我太无知到底层MVC的东西弄清楚如何去适应这个做什么,我需要。理想情况下,我想简单地用下划线代替连字符,这将是一个足够好的解决方案。如果有更好的一个,那么我感兴趣的听。

欲处理的URL的一个例子是这样的:

 的http://本地主机/应用/人/单子第一名称=鲍勃和放大器;我的年龄= 3

这个控制器:

 公众的ActionResult列表(将First_Name串,诠释My_Age)
{
    {...}
}

要再说一遍,我不能改变所产生的查询字符串,所以我需要用我的控制器来支持它在某种程度上。但如何?

有关参考,下面是正在使用从SO问题,我上面提到,我们也许能够修改来完成我想要的东西处理控制器名称,下划线和动作名称自定义RouteHandler:

 公共类HyphenatedRouteHandler:MvcRouteHandler
{
    保护覆盖的IHttpHandler GetHttpHandler(RequestContext的的RequestContext)
    {
        。requestContext.RouteData.Values​​ [控制器] = requestContext.RouteData.Values​​ [控制器]的ToString()更换( - ,_)。
        。requestContext.RouteData.Values​​ [行动] = requestContext.RouteData.Values​​ [行动]的ToString()更换( - ,_)。
        返回base.GetHttpHandler(RequestContext的);
    }
}


解决方案

你试过 [装订(preFIX =直呼其名)] ?它可能工作...

一种方法是使用自定义模型粘合剂。另一种方法是用一个动作滤波器。如果你想这样做在特定的类型使用模型绑定。如果你想这样做在特定的行动或控制器使用动作过滤器因此,对于后一种方法,你可以这样做:

 公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
    {
        变种键= filterContext.HttpContext.Request.QueryString.AllKeys.Where(K => k.Contains(' - '));
        的foreach(在键VAR K)
        {
            filterContext.ActionParameters.Add(
                新KeyValuePair<字符串对象>(
                    k.Replace(' - ','_'),filterContext.HttpContext.Request.QueryString [K]));
        }
        base.OnActionExecuting(filterContext);
    }

I'm working with some third-party software that creates querystring parameters with hyphens in their names. I was taking a look at this SO question and it seems like their solution is very close to what I need but I'm too ignorant to the underlying MVC stuff to figure out how to adapt this to do what I need. Ideally, I'd like to simply replace hyphens with underscores and that would be a good enough solution. If there's a better one, then I'm interested in hearing it.

An example of a URL I want to handle is this:

http://localhost/app/Person/List?First-Name=Bob&My-Age=3

with this Controller:

public ActionResult List(string First_Name, int My_Age)
{
    {...}
}

To repeat, I cannot change the querystring being generated so I need to support it with my controller somehow. But how?

For reference, below is the custom RouteHandler that is being used to handle underscores in controller names and action names from the SO question I referenced above that we might be able to modify to accomplish what I want:

public class HyphenatedRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler  GetHttpHandler(RequestContext requestContext)
    {
        requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
        requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
        return base.GetHttpHandler(requestContext);
    }
}

解决方案

Have you tried [Bind(Prefix="First-name")]? It might work...

One way would be with a custom model binder. Another way would be with an action filter. Use the model binder if you want to do this on a specific type. Use the action filter if you want to do this on a specific action or controller. So for the latter method you could do something like:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var keys = filterContext.HttpContext.Request.QueryString.AllKeys.Where(k => k.Contains('-'));
        foreach(var k in keys)
        {
            filterContext.ActionParameters.Add(
                new KeyValuePair<string, object>(
                    k.Replace('-', '_'), filterContext.HttpContext.Request.QueryString[k]));
        }
        base.OnActionExecuting(filterContext);
    }

这篇关于处理MVC2变量,在他们的名字连字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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