Using a dash (-) in ASP.MVC parameters

后端 未结 5 1460
失恋的感觉
失恋的感觉 2020-12-05 08:26
<% using (Html.BeginForm(\"SubmitUserName\")) { %>
    
    
<         


        
相关标签:
5条回答
  • 2020-12-05 08:57

    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.

    0 讨论(0)
  • 2020-12-05 09:01

    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)
        {
    
    0 讨论(0)
  • 2020-12-05 09:03

    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' />
    <% } %>
    
    0 讨论(0)
  • 2020-12-05 09:08

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

    public ActionResult SubmitUserName()
    {
        string userName = Request.Params["user-name"];
        ...
    }
    
    0 讨论(0)
  • 2020-12-05 09:08

    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) {...}
    
    0 讨论(0)
提交回复
热议问题