ASP.NET MVC3 經過Url傳多個參數方法

今天在MVC中,試圖用Request.QueryString["type"]取到URL中參數type的值,卻發現怎麼也取不值。仔細檢查個人路由配置:html

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Default", // 路由名稱
"{controller}/{action}/{id}/{type}", // 帶有參數的 URL
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
type= UrlParameter.Optional
} // 參數默認值
);
}



雖然說有稍許的改動,但也沒有錯誤,URL也是能正確解析。調試到程序中測試,發現無論怎麼弄,Request.QueryString.Count老是等於0,也就是說,Request.QueryString中一直沒有值,唉~崩潰了。

通過一上午的努力,終因而找到了問題的解決方法,咱們能夠經過如下兩種方法來獲取到URL中參數的值,但爲何Request.QueryString取不到值的具體緣由不知道,什麼狀況下Request.QueryString纔會有值等也不清楚,Asp.MVC本人也沒有深究,只是想多瞭解一些asp.net新技術,因此也不打算深究。

下面直接講如何在MVC中取到URL參數值的兩種方法吧:
1,該方法也是最簡單的方法:
經過Request.RequestContext.RouteData.Values["參數名"]來獲取。
本人猜測,該方法是解析出路由中的各參數的值吧,由於我在路由中有配置{controller}/{action}/{id}/{type},因此使用Request.RequestContext.RouteData.Values["type"],就能取到url中type的值了。

本人的示例中取id與type參數值的方法以下:asp.net

private int GetUrlID()
{
int ID = 0;
if (Request.RequestContext.RouteData.Values["id"] != null)
{
int.TryParse(Request.RequestContext.RouteData.Values["id"].ToString(), out ID);
}
return ID;
}

private string GetUrlType()
{
string type = string.Empty;
if (Request.RequestContext.RouteData.Values["type"] != null)
{
type = Request.RequestContext.RouteData.Values["type"].ToString();
}
return type;
}


經本人測試,是能正常取到值的。

2,在action方法中定義路由中對應的參數,如本例如,可以下定義action方法:測試

[HttpGet]
public ActionResult SupplierEdit(int ID,string Type)
{
ViewData["ID"] = ID;
ViewData["Type"] = Type;
return View();
}


這樣,系統就會自動將ID與Type的值傳到action方法中來的了,咱們就能夠在Views文件夾下的前臺文件中使用ViewData["ID"]與ViewData["Type"]來取到url中ID,Type的值了。經測試,這個方法也是行的通的。固然,在前臺文件中使用<%:Request.RequestContext.RouteData.Values["type"]% >一樣能夠取到值。

以上爲本人取Request.QueryString值的兩個小方法,由於本身對Asp.MVC瞭解並不太多,因此若是有錯誤的地方,請你們在評論中指正。ui

 

 

MVC3經過URL傳值,通常狀況下都會遇到【從客戶端(&)中檢測到有潛在危險的 Request.Path 值】的問題this

這個問題的解決方法,個人其餘博文已經有了說明,這裏給出鏈接;【從客戶端(&)中檢測到有潛在危險的 Request.Path 值】解決方法url

 

方法一:spa

Url傳參是經過Get的方式,通常咱們都是經過必定規則的Url來傳參。好比下面的URL。.net

http://localhost/contorller/action/?Params1=a&Params2=b調試

 注意:URL裏面的「?」不能去掉哦,我曾經將URL路由和url參數混淆,就是上面的URL裏面沒有「?」,搞了2天時間才弄清楚問題出在哪裏。你們可不要犯一樣的錯誤哦。code

咱們能夠在controller中經過綁定方法的方式來進行獲取,代碼以下:

 

[csharp]  view plain copy
 
  1. public ActionResult Index(ExpModel model, string Params1 , string Params2)  
  2. {  
  3.             ViewBag.P1 = Params1 ;  
  4.             ViewBag.P2= Params2;   
  5.             return View();  
  6. }  

 

方法二:

修改MVC3中的路由規則

在Global.asax.cs中,修改路由規則

[csharp]  view plain copy
 
  1. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  2.            routes.MapRoute(  
  3.                "Default"// 路由名稱  
  4.                "{controller}/{action}/{id}"// 帶有參數的 URL  
  5.                new { controller = "Home", action = "Index", id = UrlParameter.Optional} // 參數默認值  
  6.            );  

 

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:這個是用於設置路由搜索的控制器命名空間!

 

好比,咱們能夠修改成下面的規則

[csharp]  view plain copy
 
  1. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  2.            routes.MapRoute(  
  3.                "Default"// 路由名稱  
  4.                "{controller}/{action}/{uid}_{token}_{others}.html"// 帶有參數的 URL  
  5.                new { controller = "Home", action = "Index", uid = UrlParameter.Optional, token = UrlParameter.Optional,others = UrlParameter.Optional} // 參數默認值  
  6.            );  

 

若是訪問的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

相關文章
相關標籤/搜索