2015-11-06 136 views
1

我是ASP.Net MVC的新手。我已經實現路由爲:ASP.Net MVC:傳遞參數爲URL

routes.MapRoute(
    name: "SearchResult", 
    url: "{controller}/{action}/{depId}/{empId}", 
    defaults: new { controller = "Search", action = "Result" } 
); 

這是我如何調用搜索‘控制器「的結果行動’:

public ActionResult Index(SearchQueryModel _objQueryStringModel) 
{ 
    return RedirectToAction("Result", "Search", new { depId = "1", empId = "2"}); 
} 

它重定向我

localhost/Search/Result/?depId=1&empId=2 

但我想它是這樣的:

localhost/Search/Result/1/2 

我如何實現正確的路由/重定向?

+0

這是RouteConfig文件中的第一條路線嗎? –

+0

是的。此後指定默認路由。 – user1640256

+0

我剛剛嘗試過你的路線和代碼,它爲我工作。 – Ric

回答

0

你想重定向路由,而不只是重定向,用你的路線名稱有:

return RedirectToRoute("SearchResult", new { depId = "1", empId = "2" }); 

目前,您的代碼使用的缺省路由。

+0

這會將我重定向到http:// localhost/Home/Index/1/2 – user1640256

+0

此路由是否在某個區域中指定? – Luke

+0

不,我已經寫在RouteConfig.cs下的App_Start – user1640256