MVC設(shè)定默認(rèn)路由為指定的Area下的某個(gè)action
MVC中,一般的情況下,使用IDE工具幫我們生成的代碼,在路由注冊(cè)的時(shí)候:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "WebShow", action = "List", id = UrlParameter.Optional }
);
}默認(rèn)給的路由地圖 我們?cè)赿efault:中去配置 默認(rèn)是又那個(gè)控制器返回action。
這樣形如:http://localhost:1450/ 這樣,會(huì)默認(rèn)使用默認(rèn)的view文件夾中去找對(duì)應(yīng)的響應(yīng)model頁(yè)面;
但是當(dāng)項(xiàng)目規(guī)模龐大,使用了MVC提供的Area模塊快的規(guī)劃,又要讓Area塊下某一個(gè)頁(yè)面作為默認(rèn)頁(yè)面時(shí),那就需要把route默認(rèn)的路由去轉(zhuǎn)到我們指定Area下;要用到RouteCollection 的DataTokens方法。
舉個(gè)例子:Area下有注冊(cè)一個(gè)Admin塊
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Login", action = "Index", id = UrlParameter.Optional } //默認(rèn)路徑是Login控制器,和Index action
);
}
}如果要讓默認(rèn)到admin下指定的。那么RegisterRoutes方法需要給routes路由到area 下的Admin塊;
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Login", action = "Index", id = UrlParameter.Optional }, //這里要和Admin塊下的默認(rèn)控制器和action一樣
new[] { "GTA.CMS.Site.Web.Areas.Admin.LoginController" }// 這個(gè)是你控制器所在命名空間
).DataTokens.Add("area","Admin");
}總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C++中const的實(shí)現(xiàn)細(xì)節(jié)介紹(C,C#同理)
本篇文章主要是對(duì)C++中const的實(shí)現(xiàn)細(xì)節(jié)進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
C#使用DeflateStream解壓縮數(shù)據(jù)文件的方法
這篇文章主要介紹了C#使用DeflateStream解壓縮數(shù)據(jù)文件的方法,較為詳細(xì)的分析了DeflateStream方法對(duì)文件進(jìn)行壓縮及解壓縮的步驟與技巧,需要的朋友可以參考下2015-04-04
C#基于Twain協(xié)議調(diào)用掃描儀,設(shè)置多圖像輸出模式(Multi image output)
這篇文章主要介紹了C#基于Twain協(xié)議調(diào)用掃描儀,設(shè)置多圖像輸出模式(Multi image output)的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01

