ASPNET MVC URL、路由及區域
一、URL、路由及區域
一、 配置路由器
1、 URL模式
2、 定義路由默認值
3、 使用靜態URL
4、 Route順序
5、 自定義段變量
6、 使用action方法參數
7、 通過瀏覽器傳參數
1、 在路由表中定義路由,需要對參數命名
2、 獲取參數值:
1)、通過控制器中的action方法,如List(string cate),需要方法中的參數名和路由表中的參數相同,RouteData.Values[Key]獲取,key為參數名,如果獲取的是對象,可以通過模型綁定機制實現
二、 生成輸出的URLS
1) 視圖中靜態鏈接
1、ActionLink:對應控制器/動作,使用默認路由
參數:new{參數=值,…}
樣式:new{@class=樣式名}
2、RouteLink:使用指定路由
格式:<a/>:控制器/動作/參數
2) 動態轉向
1、 Redirect(url):參數是url地址
2、RedirecrToAction(action,Name,controllerName),參數為動作名和各種控制名
3、 RedirectToRoute(routeName,routeValues),轉向到指定路由
routeName:路由名
routeValues=new{controller=value,action=value,id=value}
二、結合第一章MVC內容,設置產品分類顯示
1、在"MVCProduct"項目單擊右鍵添加“區域”,名字設為“Areas”
如圖所示:
2、在“Areas”文件里面的“Controller”文件添加“控制器”名為“HomeController”
如圖所示:
3、繼續在“Areas”文件里面的“Controller”文件添加“控制器”名為“HomeController.cs”頁面方法里"Index()"添加一個視圖
如圖所示:
三、設置”路由“添加路由參數“
如圖所示:
四、給"路由"添加對應"控制器名字",名字可以在"Controller"文件里面"HomeController.cs"頁面
如圖所示:
五、復制名字后,把它放在"路由參數里面"
代碼示例:

using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcProduct { // 注意: 有關啟用 IIS6 或 IIS7 經典模式的說明, // 請訪問 http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "cate", // 路由名稱 "{controller}/{action}/{cate}", // 帶有參數的 URL new { controller = "Home", action = "Index", cate = UrlParameter.Optional }, // 參數默認值 new[] { "MvcProduct.Controllers" } ); routes.MapRoute( "Default", // 路由名稱 "{controller}/{action}/{id}", // 帶有參數的 URL new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 參數默認值 new[] { "MvcProduct.Controllers" } ); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); // 默認情況下對 Entity Framework 使用 LocalDB Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True"); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } } }
如圖所示:
六、在"表示層"添加一個根據名字分類方法
代碼示例:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using LinqService; //引用 namespace LinqBLL { public class ProductBll { public List<Product> GetProduct() { using (SportsStoreEntities se = new SportsStoreEntities()) { var products = se.Product; //數據庫里面表名稱 return products.ToList(); } } //通過分類名獲取體育產品列表 public List<Product> GetProductByCateName(string cate) { using(SportsStoreEntities se=new SportsStoreEntities()) { if (string.IsNullOrEmpty(cate)) { var products=se.Product; return products.ToList(); } else //查找對應分類數據 { var books = from a in se.Product where a.Name.Contains(cate) select a; return books.ToList(); } } } } }
如圖所示:
七、最后在"MvcProduct"項目調用方法可以了,把之前方法注釋掉
代碼示例:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using LinqBLL; using LinqService; namespace MvcProduct.Controllers { public class HomeController : Controller { ProductBll bll = new ProductBll(); public ActionResult Index() { ViewBag.Message = "歡迎使用 ASP.NET MVC!"; return View(); } ////獲取產品 //public ActionResult List() //{ // List<Product> ps = bll.GetProduct(); // return View(ps); //} public ActionResult List(string cate) { List<Product> ps = bll.GetProductByCateName(cate); return View(ps); } public ActionResult About() { return View(); } } }
如圖所示:
八、運行結果:輸入要分類名稱就可以了