通过Url传多个参数方法
MVC3通过URL传值,一般情况下都会遇到【从客户端(&)中检测到有潜在危险的 Request.Path 值】的问题
这个问题的解决方法,我的其他博文已经有了说明,这里给出连接【从客户端(&)中检测到有潜在危险的 Request.Path 值】解决方法
方法一:
Url传参是通过Get的方式,一般我们都是通过一定规则的Url来传参。比如下面的URL。
http://localhost/contorller/action/?Params1=a&Params2=b
注意:URL里面的“?”不能去掉哦,我曾经将URL路由和url参数混淆,就是上面的URL里面没有“?”,搞了2天时间才弄清楚问题出在哪里。大家可不要犯同样的错误哦。
我们可以在controller中通过绑定方法的方式来进行获取,代码如下:
public ActionResult Index(ExpModel model, string Params1 , string Params2)
{
ViewBag.P1 = Params1 ;
ViewBag.P2= Params2;
return View();
}
方法二:
修改MVC3中的路由规则
在Global.asax.cs中,修改路由规则
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional} // 参数默认值
);
MapRoute方法在RouteCollectionExtensions里有6个重载版本!在这里我挑了一个参数最多的重载版本来进行介绍
public static Route MapRoute(
this RouteCollection routes,
string name,
string url,
Object defaults,
Object constraints,
string[] namespaces
)
name:路由在路由列表里的唯一名字(两次MapRoute时name不能重复)
url:路由匹配的url格式
defaults:路由url {占位符} 的默认值
constraints:url的 {占位符} 的约束
namespaces:这个是用于设置路由搜索的控制器命名空间!
比如,我们可以修改为下面的规则
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{uid}_{token}_{others}.html", // 带有参数的 URL
new { controller = "Home", action = "Index", uid = UrlParameter.Optional, token = UrlParameter.Optional,others = UrlParameter.Optional} // 参数默认值
);
如果访问的URL地址如:http://localhost/home/index/123_tokenvalue_othersvalue.html 时
controller="Home", action="Index", uid=123, token=tokenvalue, others=othersvalue
获取和上面的方法一样。
关于Route 的详细用法和说明,大家看MSDN 上的资料吧,这里给个连接:
ASP.NET Routing:http://msdn.microsoft.com/en-us/library/cc668201.aspx?cs-save-lang=1&cs-lang=csharp
通过Url传多个参数方法的更多相关文章
- ASP.NET MVC3 通过Url传多个参数方法
MVC3通过URL传值,一般情况下都会遇到 [从客户端(&)中检测到有潜在危险的 Request.Path 值]的问题 这个问题的解决方法,我的其他博文已经有了说明,这里给出连接 ; [从客户 ...
- 利用url传多个参数
刚开始接触jsp,比较偏向于用button标签的onclick方法进行页面的跳转.但是关于页面跳转的各种问题真是叫人头大,以下记录,仅仅为自己以后查看. Qone 用url传参的时候遇到中文怎么办 编 ...
- MVC中URL传多个参数
1.mvc中url传递多个参数不能直接使用&,会报错(从客户端(&)中检测到有潜在危险的 Request.Path 值) 方法①:使用?---/Home/Index/?id=xxx&a ...
- js取url问号后的参数方法封装
工具方法: function getRequest() { var url = location.search; // 获取url中?后面的字符串 var theRequest = new Objec ...
- js url传参,参数加密
前台 function encode64(input) { var output = ""; var base = new Base64(); var output = base. ...
- js获得url传过来的参数
function getParam(url) { var arr = url.split('?'); //取?以后 var a ...
- javaScript获取url问号后面的参数
javaScript获取url问号后面的参数方法 function GetRequest() { var url = location.search; //获取url中"?"符后的 ...
- URL传参时中文参数乱码的解决方法
URL传参时,中文参数乱码的解决: 今天在工作中遇到了这样的一个问题,在页面之间跳转时,我将中文的参数放入到url中,使用location进行跳转传参,但是发现接收到的参数值是乱码.我的代码是这样写的 ...
- js的form表单提交url传参数(包含+等特殊字符)的解决方法
方法一:(伪装form表单提交) linkredwin = function(A,B,C,D,E,F,G){ var formredwin = document.createElemen ...
随机推荐
- 武汉科技大学ACM:1010: 零起点学算法27——判断是否直角三角形
Problem Description 输入三个整数,分别代表三角形的三条边长度,判断能否构成直角三角形 Input 输入3个整数a,b,c(多组数据,-5000000<a,b,c<500 ...
- Linux入门 (笔记)
Man can conquer nature. "人定胜天" 一.基本操作 1.重要快捷键 Tab 补全命令.目录.参数.文件名等 Ctrl+c 强制终止当前的程序 Ctrl+ ...
- C++ AO读取shapefile的属性值
C++ AO读取一个shapefile文件的所有属性值 #include "stdafx.h" #include "iostream.h" #inc ...
- 魔法方法:算术运算 - 零基础入门学习Python042
魔法方法:算术运算 让编程改变世界 Change the world by program 我现在重新提一个名词:工厂函数,不知道大家还有没有印象?我们在老早前就提到过Ta,由于那时候我们还没有学习类 ...
- Xcode中将图片放入Images.xcassets和直接拖入的区别
将图片放入Images.xcassets 在mainBundle里面Xcode会生成一个Assets.car文件,将我们放在Images.xcassets的图片打包在里面.(程序会变大(?)) 无论是 ...
- Thread 1 cannot allocate new log的问题分析
http://blog.csdn.net/zonelan/article/details/7613519 http://leoguan.blog.51cto.com/816378/584494 htt ...
- TextView属性详解
android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/all)android:a ...
- Microsoft Visual Studio 6.0 Enterprise Edition
我们的老古董啊 啊啊啊 啊啊 <Microsoft Visual Studio 6.0 Enterprise Edition>(完整9CD,带中文MSDN& <Micr ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- HDOJ 1323 Perfection(简单题)
Problem Description From the article Number Theory in the 1994 Microsoft Encarta: "If a, b, c a ...