代码之家  ›  专栏  ›  技术社区  ›  THX-1138

在ASP.MVC参数中使用破折号(-)

  •  10
  • THX-1138  · 技术社区  · 14 年前
    <% using (Html.BeginForm("SubmitUserName")) { %>
        <input type='text' name='user-name' />
        <input type='submit' value='Send' />
    <% } %>
    

    接受的相应操作方法的签名应该是什么 user-name 参数?

    public ActionResult SubmitUserName(string user-name) {...}
    

    上面的方法签名由于某种原因不起作用;-)

    我知道有一个 ActionNameAttribute 用破折号处理情况。有什么像 ParameterNameAttribute ?

    4 回复  |  直到 12 年前
        1
  •  13
  •   Andy Wilson    14 年前

    正如每个人都注意到的,最简单的解决方法是不要使用破折号。如果您真的需要破折号,那么您可以创建自己的actionfilterattribute来处理它。

    类似:

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class ParameterNameAttribute :  ActionFilterAttribute
    {
        public string ViewParameterName { get; set; }
        public string ActionParameterName { get; set; }
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if(filterContext.ActionParameters.ContainsKey(ViewParameterName))
            {
                var parameterValue = filterContext.ActionParameters[ViewParameterName];
                filterContext.ActionParameters.Add(ActionParameterName, parameterValue);   
            }
        }
    }
    

    然后将过滤器应用于适当的操作方法:

    [ParameterName( ViewParameterName = "user-data", ActionParameterName = "userData")]
    [ParameterName( ViewParameterName = "my-data", ActionParameterName = "myData" )]
        public ActionResult About(string userData, string myData)
        {
            return View();
        }
    

    您可能希望增强parameternameattribute以处理大小写,但这是基本的想法。

        2
  •  11
  •   Edward Brey    12 年前

    在action方法的第一行中创建伪参数:

    public ActionResult SubmitUserName()
    {
        string userName = Request.Params["user-name"];
        ...
    }
    
        3
  •  3
  •   ProVega    12 年前

    我发现这个答案很有帮助,但我不知道所提供的示例有什么帮助。它似乎只是“重命名”一个活页夹提供的值。

    在我的例子中,有一个外部服务将我发布“body plain”之类的内容,我无法控制名称。所以我修改了这个示例,如下所示:

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class ParameterNameMapAttribute : ActionFilterAttribute
    {
        public string InboundParameterName { get; set; }
        public string ActionParameterName { get; set; }
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            object value = filterContext.RequestContext.HttpContext.Request[InboundParameterName];
    
            if (filterContext.ActionParameters.ContainsKey(ActionParameterName))
            {
                filterContext.ActionParameters[ActionParameterName] = value;
            }
            else
            {
                throw new Exception("Parameter not found on controller: " + ActionParameterName);
            }
        }
    }
    

    例如,它实际上接受参数“body plain”,并将其映射到我在控制器上定义的actionParameter。像这样:

    [ParameterNameMap(InboundParameterName = "body-plain", ActionParameterName = "bodyPlainText")]
        [ParameterNameMap(InboundParameterName = "Thread-Topic", ActionParameterName = "alternateSubject")]
        public HttpStatusCodeResult Process(string token, string timestamp, string signature, string subject, string sender, string recipient, string bodyPlainText, string alternateSubject)
        {
    
        4
  •  1
  •   Buildstarted    14 年前

    我建议这样做-除非需要使用 user-name 属性(或者您没有绑定到模型)

    <% using (Html.BeginForm("SubmitUserName")) { %>
        <%: Html.TextBoxFor(m => m.UserName) %>
        <input type='submit' value='Send' />
    <% } %>