在我的MVC網絡應用程序中,我將所有網址與一個網址段路由到相同的控制器操作。例如像:如何將網址段作爲參數發送到操作方法?
http://example.com/onePage
這裏的控制器我的操作方法:
public ActionResult SomeAction(string urlSegment)
{
...
}
現在,我想要的網址段(如從例如「onePage」),將作爲輸入發送到行動方法。
你能說明MapRoute應該是什麼樣子才能做到這一點嗎?
在我的MVC網絡應用程序中,我將所有網址與一個網址段路由到相同的控制器操作。例如像:如何將網址段作爲參數發送到操作方法?
http://example.com/onePage
這裏的控制器我的操作方法:
public ActionResult SomeAction(string urlSegment)
{
...
}
現在,我想要的網址段(如從例如「onePage」),將作爲輸入發送到行動方法。
你能說明MapRoute應該是什麼樣子才能做到這一點嗎?
您應該能夠通過編輯RouteConfig.cs文件來做到這一點:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "Home|Account" }
);
routes.MapRoute(
"CustomRoute",
"{*urlSegment}",
new { controller = "MyController", action = "SomeAction" }
);
}
變化MyController
到任何控制器的名字叫做。
如果只想匹配一個段,請刪除*
。
如果請求是完全匹配一個'urlSegment' ,那麼不應該那個'*'被刪除? –
它可能看起來像這樣。
routes.MapRoute(
"UrlSegment", // Route name
"{urlSegment}", // URL with parameters
new { action="SomeAction",controller="ControllerName" }, // parameter defaults
new[] { "Namespace.Controllers" } // controller namespaces);
我在我的手機上,所以不幸無法驗證,但應該得到你要找的東西。
你看過這個:http://stackoverflow.com/questions/1601211/asp-net-mvc-routing-for-a-single-controller-site –