ASP.NET MVC第023天_Ajax Helper_Ajax.RouteLink使用筆記part3
Ajax.RouteLink() 跟 Ajax.ActionLink() 兩者皆為產生出超連結的 <a> tag
只不過產生方式不同
Ajax.RouteLink() 別於 Ajax.ActionLink() 可支援直接設置特定Controller跟Action
是只可以透過設置路由模板名稱(路由虛擬路徑)的方式進行
以下是Ajax.RouteLink()的多載
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, object routeValues, AjaxOptions ajaxOptions);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, RouteValueDictionary routeValues, AjaxOptions ajaxOptions);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions, object htmlAttributes);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, object routeValues, AjaxOptions ajaxOptions);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions);
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes);
那其實就是比ActionLink多出一個routeName參數
路由預設就是在之前文章介紹過的
預設MVC專案路由會在App_Start 目錄的RouteConfig.cs中
這裡我們多客製了一條路由
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcAjaxTestApp { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "News_Blog", url: "News_Blog/{controller}/{id}/{action}-{name}", defaults: new { Controller = "Me", action = "Ax", id = UrlParameter.Optional } ); } } } |
比之前具變化性一點
沒有routeName參數情況
~/Home/Index.chtml
1 2 3 4 5 6 7 8 9 | @{ ViewBag.Title = "Home Page"; } <div class="row"> <div class="col-md-4"> @Ajax.RouteLink("張三", new { controller = "My", action = "Photo" }, new AjaxOptions() { HttpMethod = "Get" }) </div> </div> |
而RouteLink在使用不具routeName參數的多載方法時
預設其實就是抓RouteConfig.cs中第一種路由設置模式
這裡調整路由的上下順序再來觀察
會看到預設抓第一種的路由模式
有routeName參數情況
這裡調整View呼叫的程式碼
~/Home/Index.chtml
這裡指定的路由都必須是有定義在RouteConfig.cs中的
這裡指定News_Blog會導向該路由
改指定Default會導向預設路由
留言
張貼留言