操作参数命名

扫码查看
本文介绍了操作参数命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用提供的默认路由,我不得不说出我的参数ID。那好了很多我的控制器动作,但我想用在某些地方一些更好的变量命名。是否有某种属性的,我可以用,这样我可以在我的行动签名更有意义的变量名?

  //默认路由:
routes.MapRoute(
  默认,//路线名称
  {控制器} / {行动} / {ID},// URL带参数
  新{控制器=家,行动=索引,ID =} //参数默认
);//行动签名:
公众的ActionResult ByAlias​​(字符串化名)
{
  //因为路由指定ID,因此这个动作需要一个别名,没有什么是绑定
}


解决方案

使用[绑定]属性:

 公众的ActionResult ByAlias​​([装订(preFIX =ID)的字符串别名){
    //你的code在这里
}

Using the default route provided, I'm forced to name my parameters "id". That's fine for a lot of my Controller Actions, but I want to use some better variable naming in certain places. Is there some sort of attribute I can use so that I can have more meaningful variable names in my action signatures?

// Default Route:
routes.MapRoute(
  "Default",                                              // Route name
  "{controller}/{action}/{id}",                           // URL with parameters
  new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

// Action Signature:
public ActionResult ByAlias(string alias)
{
  // Because the route specifies "id" and this action takes an "alias", nothing is bound
}
解决方案

Use the [Bind] attribute:

public ActionResult ByAlias([Bind(Prefix = "id")] string alias) {
    // your code here
}

这篇关于操作参数命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 19:33
查看更多