ASP.NET MVC Area操作

來源:互聯網
上載者:User

ASP.NET MVC Area操作

* 1、建立 Area:右鍵 -> Add -> Area...
* 2、繼承 AreaRegistration,配置對應此 Area 的路由
* 3、在 Global 中通過 AreaRegistration.RegisterAllAreas(); 註冊此 Area
* 4、有了 Area,就一定要配置路由的命名空間

using System.Web.Mvc;

namespace MVC20.Areas.AsynchronousController

{

// 建立一個 Area 會自動產生這個繼承自 AreaRegistration 的類

// 如果需要使用此 Area 下的 MVC, 需要在 Global 中 AreaRegistration.RegisterAllAreas();

public class AsynchronousControllerAreaRegistration : AreaRegistration

{

public override string AreaName

{

get

{

return "AsynchronousController";

}

}

public override void RegisterArea(AreaRegistrationContext context)

{

// 在 Area 中配置路由的時候,要設定命名空間(即本例中的第 4 個參數)

context.MapRoute(

"AsynchronousController_default",

"AsynchronousController/{controller}/{action}/{id}",

new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults

new string[] { "MVC20.Areas.AsynchronousController.Controllers" }

);

}

}

}

Global.asax

代碼

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

namespace MVC20

{

public class MvcApplication : System.Web.HttpApplication

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// 用於本項目中使用了 Area,所以在配置路由的時候,要設定命名空間(即本例中的第 4 個參數)

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{id}", // URL with parameters

new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // UrlParameter.Optional - 如果從url路由中無法擷取某個參數的值,則從url參數中擷取該參數的值

new string[] {"MVC20.Controllers"}

);

}

protected void Application_Start()

{

// 註冊本應用程式中的所有 Area

AreaRegistration.RegisterAllAreas();

RegisterRoutes(RouteTable.Routes);

}

}

}

'mso-�-o83���0.5pt; font-family:"Segoe UI","sans-serif";color:black;background:#DDEDFB'> false;

}

return true;

}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.