[英]ASP.NET MVC 5 Url Rewriting to encompass all actions in controller?
我想知道是否可以在Route.Config文件中編寫一條規則,該規則可以包含單個控制器中的所有操作? 我已經讀過這篇文章,但它有點高於我(我剛開始使用url重寫和路由,術語不熟悉)。
我設法將我的一個動作從mydomain.co.za/Trainee/Action?id=123
更改為mydomain.co.za/Trainee/Action/123
但我希望能夠涵蓋實習生控制器中的所有動作,這樣你就可以有一條規則來制作Trainee/Action1/123
或Trainee/Action2/123
Trainee/Action1/123
這是我用過的代碼:
routes.MapRoute(
name: "ActionRewrite",
url: "Trainee/Action/{id}",
defaults: new { controller = "Trainee", action = "Action" }
);
另外,是否可以隱藏URL中的參數,這樣無論用戶在做什么,您都可以使用mydomain.co.za/Action/?
嘗試這個:
routes.MapRoute(
name: "ActionRewrite",
url: "Trainee/{action}/{id}",
defaults: new { controller = "Trainee", action = {action} }
);
請記住,您可以使用通配符來匹配您的網址。
如果您願意,可以指定{id}參數保持可選,這意味着它也將匹配未指定參數的操作。
嘗試這個.. Url重新使用自定義ID和名稱。 添加應用啟動路由配置。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "contactus",
url: "contactus",
defaults: new { Controller = "Cms", action = "Index", id = (int)Common.CMSContactUs },
namespaces: new[] { "QZero.Controllers" }
);
routes.MapRoute(
name: "aboutus",
url: "aboutus",
defaults: new { Controller = "Cms", action = "Index", id = (int)Common.CMSAboutUs },
namespaces: new[] { "QZero.Controllers" }
);
routes.MapRoute(
name: "useragreement",
url: "useragreement",
defaults: new { Controller = "Cms", action = "Index", id = (int)Common.CMSUserAgreement },
namespaces: new[] { "QZero.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "QZero.Controllers" }
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.