14
<% using (Html.BeginForm("SubmitUserName")) { %>
    <input type='text' name='user-name' />
    <input type='submit' value='Send' />
<% } %>

What should be a signature of a corresponding Action method to accept user-name parameter?

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

Method signature above does not work for some reason ;-)

I know there is an ActionNameAttribute to handle situation with a dash in action name. Is there something like ParameterNameAttribute?

THX-1138
  • 21,316
  • 26
  • 96
  • 160

5 Answers5

13

Create a pseudo-parameter in the first line of the action method:

public ActionResult SubmitUserName()
{
    string userName = Request.Params["user-name"];
    ...
}
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
13

As everyone has noted, the easiest fix would be not to use a dash. If you truly need the dash, you can create your own ActionFilterAttribute to handle it, though.

Something like:

[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);   
        }
    }
}

You would then apply the filter to the appropriate Action method:

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

You would probably want to enhance the ParameterNameAttribute to handle upper/lower case, but that would be the basic idea.

Andy Wilson
  • 1,383
  • 9
  • 15
  • `OnActionExecuting` should use `TryGetValue` to avoid the double lookup of `ContainsKey` followed by the indexer. – Edward Brey Apr 13 '16 at 13:02
8

Not answering the actual question based on the technlogy in question, but anyway, the world moves forward in some areas; in AspNetCore.Mvc you can simply do:

    [HttpGet()]
    public ActionResult SubmitUserName( [FromHeader(Name = "user-Name")] string userName) {...}
Caad9Rider
  • 654
  • 1
  • 8
  • 16
3

I found this answer helpful, but I don't know exactly how the provided example helps. It appears to just "rename" a value that the binder all ready provided.

In my case, I was being posted to by an external service that would post something like "body-plain" and I could not control the name. So I modified this sample to look like this:

[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);
        }
    }
}

This actually takes in the parameter "body-plain" for example and maps it to an ActionParameter I defined on my controller. Like so:

[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)
    {
ProVega
  • 5,864
  • 2
  • 36
  • 34
1

I would suggest doing something like this - unless it's required to use the user-name attribute (or you're not binding to a model)

<% using (Html.BeginForm("SubmitUserName")) { %>
    <%: Html.TextBoxFor(m => m.UserName) %>
    <input type='submit' value='Send' />
<% } %>
Buildstarted
  • 26,529
  • 10
  • 84
  • 95