简体   繁体   中英

Using a dash (-) in ASP.MVC parameters

<% 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 ?

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.

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

public ActionResult SubmitUserName()
{
    string userName = Request.Params["user-name"];
    ...
}

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)
    {

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' />
<% } %>

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) {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM