2015-11-06 139 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