MVC中Url请求与控制器的默认约定

1.请求的url如:http://localhost:52481/Home/Browse?genre=1
控制器方法:
public string Browse(string genre) //这里返回值为string(不是ActionResult),但依然能执行该方法
{
string message = HttpUtility.HtmlEncode("传的参数genre为:" + genre);//利用HttpUtility.HtmlEncode能阻止链接注入javascript或HTML代码,如下图
return message;
}
这里mvc中有个默认的约定,会把查询的字符串genre作为操作方法的参数来接收(大小写不影响)

2.请求的url如:http://localhost:52481/Home/Browse/1
控制器方法:
public string Browse(string id) //这里返回值为string(不是ActionResult),但依然能执行该方法
{
string message = HttpUtility.HtmlEncode("传的参数genre为:" + genre);//利用HttpUtility.HtmlEncode能阻止链接注入javascript或HTML代码,如下图
return message;
}
Mvc还有分默认是,当没用查询参数的名称是,默认为名称为id,上面的网址就相当于http://localhost:52481/Home/Browse?id=1

posted @ 2014-06-05 10:36  卡萨丁·周  阅读(403)  评论(0编辑  收藏  举报