ASP.NET MVC——URL路由

在MVC以前,ASP.NET假設請求的URL與服務器上的文件之間有關聯,服務器接受請求,並輸出相應的文件。而在引入MVC後,請求是由控制器的動做方法來處理的。爲了處理URL,便引入了路由系統。html

首先咱們來建立一個基礎項目用來演示。代碼以下:瀏覽器

1     public class HomeController : Controller
2     {
3         public ActionResult Index()
4         {
5             ViewBag.Controller = "Home";
6             ViewBag.Action = "Index";
7             return View("ActionName");
8         }
}
1     public class AdminController : Controller
2     {
3         public ActionResult Index()
4         {
5             ViewBag.Controller = "Admin";
6             ViewBag.Action = "Index";
7             return View("ActionName");
8         }
9     }

有兩個controller,它們都返回視圖「ActionName」,咱們建立一個Shared文件夾並在其中新建一個」ActionName「視圖,代碼以下:服務器

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ActionName</title>
</head>
<body>
    <div>The controller is: @ViewBag.Controller</div>
    <div>The action is: @ViewBag.Action</div>
</body>
</html>

1. 建立並註冊一條簡單的路由url

         路由是在Routefig.cs文件中定義的,咱們打開文件,會看到有一條默認的路由以下:spa

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
               );

  咱們能夠刪掉這條路由,手工來建立。例如這樣:3d

routes.MapRoute("MyRoute", "{controller}/{action}");

  咱們將URL寫爲 localhost:端口/Admin/Index,咱們能夠看到瀏覽器成功轉到了Admin控制器的Index動做方法。code

2.定義默認值htm

  當咱們以前運行程序時,會發現當URL爲根地址,會發現出錯。實際上是由於它不匹配已經定義的路由。那麼咱們修改以前的路由來爲其添加默認值。blog

 routes.MapRoute("MyRoute", "{controller}/{action}", new { controller = "Home", action = "Index" });

       如今當URL爲根地址時,瀏覽器會自動導航到咱們所定義的默認值。ip

     3.使用靜態URL

       咱們添加這樣一條路由。

routes.MapRoute("", "Public/{controller}/{action}", new { controller = "Home", action = "Index" });

       URL就會變成 localhost:端口/Public/Admin/Index。Public片斷就是靜態的。

      

Tip:這裏要提一下,路由的順序MapRoute方法會把一條路由添加到RouteCollection集合的末尾,即通常是按添加的順序被運用的。路由系統試圖根據最早被定義的路由模式來匹配輸入的URL,只有在不匹配時纔會繼續對下一條路由進行處理,直到找到匹配的一條。

4. 定義自定義片斷變量

   咱們除了使用這些內建的片斷變量,也能夠定義本身的變量,好比,新建如下路由:

 

routes.MapRoute("MyRoute", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "DefaultId"});

 

        而且修改HomeController以下:

 1         public ActionResult Index()
 2         {
 3             ViewBag.Controller = "Home";
 4             ViewBag.Action = "Index";
 5             return View("ActionName");
 6         }
 7         public ActionResult CustomVariable()
 8         {
 9             ViewBag.Controller = "Home";
10             ViewBag.Action = "CustomVariable";
11             ViewBag.CustomVariable = RouteData.Values["id"];
12             return View();
13         }

       添加視圖CustomVariable,代碼以下:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CustomVariable</title>
</head>
<body>
    <div>The controller is: @ViewBag.Controller</div>
    <div>The action is: @ViewBag.Action</div>
    <div>The custom variable is: @ViewBag.CustomVariable</div>
</body>
</html>

      啓動程序,並導航到/Home/CustomVariable/Hello,ViewBag會接受到這個自定義片斷變量的值,並將其傳遞爲視圖。

 5. 定義可變路由

    可變路由能夠以一個單一的路由,對任意長度的URL進行路由。經過一個叫作「全匹配」的片斷變量,並以「*」做爲其前綴,即可以定義對可變片斷數的支持。代碼清單以下:

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

          這條路由將匹配任何URL,不管URL包含多少片斷數,前三個片斷分別用於設置controller、action、id變量的值,若是URL含有更多片斷,則它們所有賦給catchall變量。注意,由catchall捕獲的片斷是以「片斷/片斷/片斷」的形式表示的。你須要對這個字符進行處理,把它們分解成一個一個片斷。

相關文章
相關標籤/搜索