我想從一個網址如下參數:獲取從網址asp.net的MVC參數4
www.domain.com/Controller/Action/Parameter
在我的控制器:
public ActionResult Action()
{
//some codes
}
用戶輸入網址如上,應該導航具體的控制器。我如何在我的控制器的操作中獲得「參數」?
我想從一個網址如下參數:獲取從網址asp.net的MVC參數4
www.domain.com/Controller/Action/Parameter
在我的控制器:
public ActionResult Action()
{
//some codes
}
用戶輸入網址如上,應該導航具體的控制器。我如何在我的控制器的操作中獲得「參數」?
例
public ActionResult Action()
{
string param1 = this.Request.QueryString["param"];
}
你需要兩件事情: 1)添加參數到你的動作,即: 行動(字符串PARAM,...)
2)配置路由告訴MVC如何將路線數據映射到參數。 默認mvc路由配置爲將/ ##映射到「id」參數,但您可以根據需要添加更多路由來映射其他參數。例如,這行添加到您的App_Start/RouteConfig.cs文件:
routes.MapRoute( 名稱: 「CustomRoute」,
網址:「{控制器}/{行動}/{} PARAM );
注意,「{PARAM}」匹配您的行爲參數的名稱
當有人輸入網址像上面的例子,你可以這樣做是爲了讓你的控制器段值
網址:。WWW .domain.com/Controller/Action /參數
控制器:
public ActionResult Action()
{
string parameter = this.Request.Url.Segments[segmentIndex];
//The segmentIndex in the url above should be 3
}
更改你方法包括參數 - 例如'public ActionResult Action(string id)'假設你使用默認路由(在你的例子中,'id'的值將是''參數「') –