在ASP.MVC参数中使用破折号( - )
<% using (Html.BeginForm("SubmitUserName")) { %>
<input type='text' name='user-name' />
<input type='submit' value='Send' />
<% } %>
应接受user-name
参数的相应Action方法的签名应该是什么?在ASP.MVC参数中使用破折号( - )
public ActionResult SubmitUserName(string user-name) {...}
方法签名上面并没有因为某些原因;-)
我知道有一个ActionNameAttribute
处理情况与动作名称的冲刺工作。有没有像ParameterNameAttribute
?
正如大家所指出的,在最简单的解决方法是不再使用短划线。如果你真的需要破折号,你可以创建自己的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办理大/小写,但将是基本的想法。
这在MVC 4中并不适用于我,ProVega的做法是 – 2014-01-08 01:52:38
'OnActionExecuting'应该使用'TryGetValue'来避免'ContainsKey'跟索引器的双重查找。 – 2016-04-13 13:02:10
我建议做这样的事情 - 除非它需要使用user-name
属性(或者你不绑定到一个模型)
<% using (Html.BeginForm("SubmitUserName")) { %>
<%: Html.TextBoxFor(m => m.UserName) %>
<input type='submit' value='Send' />
<% } %>
我发现这个答案有帮助,但我不确切知道如何提供的示例帮助。它似乎只是“重命名”一个已经准备好的资料夹。
在我的情况下,我被张贴到一个外部服务,将张贴像“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);
}
}
}
这实际上发生在参数“身体平原”为例,它映射到我我的控制器上定义的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)
{
在操作方法的第一行创建一个伪参数:
public ActionResult SubmitUserName()
{
string userName = Request.Params["user-name"];
...
}
这甚至没有为我编译。你在谈论ApiController吗? – 2014-01-11 18:06:01
哦,我很傻,问题是关于MVC ......而我正在做Web API。好吧。 – 2014-01-11 18:14:38
在MVC中,我工作得很好。 Upvoted。 – ChrisFox 2015-05-30 10:31:27
你真的需要使用几许? – 2010-08-11 18:04:55
是否需要**才能成为用户名? – Buildstarted 2010-08-11 18:05:27
根本不需要。令人费解的是MVC没有想到这种情况。我实际上并没有使用它,只是好奇它是否存在......或者它为什么不存在。 – 2010-08-11 19:06:51