在asp.net MVC中,「首頁」(即打到www.foo.com時顯示的路徑)被設置爲Home/Index。在Asp.Net中設置「主頁」MVC
- 該值存儲在哪裏?
- 如何更改「主頁」?
- 在家庭控制器的索引操作中,是否比使用RedirectToRoute()更優雅?
我嘗試在我的項目中尋找Home/Index,並找不到引用,也不能在IIS(6)中看到任何內容。我查看了根目錄下的default.aspx頁面,但似乎沒有做任何相關的事情。
感謝
在asp.net MVC中,「首頁」(即打到www.foo.com時顯示的路徑)被設置爲Home/Index。在Asp.Net中設置「主頁」MVC
我嘗試在我的項目中尋找Home/Index,並找不到引用,也不能在IIS(6)中看到任何內容。我查看了根目錄下的default.aspx頁面,但似乎沒有做任何相關的事情。
感謝
看那Default.aspx/Default.aspx.cs
和Global.asax.cs中
你可以設置缺省路由
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
只要改變控制器/動作名稱到您所需的默認。這應該是路由表中的最後一個路由。
檢查的RegisterRoutes方法中的global.asax.cs - 它是路由配置默認的地方......
路由在Startup
類的Configure
方法構成。要設置「主頁」,只需添加以下內容。這將導致用戶被路由到MapRoute方法中定義的控制器和操作,如果他們導航到您的網站的基本URL,即yoursite.com將路由用戶到yoursite.com/foo/index:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=FooController}/{action=Index}/{id?}");
});
使用位於App_Start/RouteConfig.cs(MVC 3和4)或Global.asax.cs(MVC 1和2)中的RegisterRoutes方法,如下所示。這將導致用戶路由到控制器和動作的圖路線法規定,如果他們瀏覽到您網站的基本URL,即yoursite.com將路由用戶yoursite.com/foo/index:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Here I have created a custom "Default" route that will route users to the "YourAction" method within the "FooController" controller.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "FooController", action = "Index", id = UrlParameter.Optional }
);
}
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 = "Your Controller", action = "Your Action", id = UrlParameter.Optional }
);
}
}
我試過了答案,但它沒有爲我工作。這是我最終做的:
創建一個新的控制器DefaultController。在索引操作中,我寫了一行重定向:
return Redirect("~/Default.aspx")
在RouteConfig.cs中,更改該路由的controller =「Default」。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
第1步:在您的解決方案中單擊Global.asax文件。
第2步:然後去的
RouteConfig.RegisterRoutes(RouteTable.Routes);
第3步定義:更改控制器名稱和視圖名稱
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 }
);
}
}
MVC之前5通過調用routes.MapRoute(...)
可以將URL映射到特定的操作和控制器在RouteConfig.cs文件中。這是存儲主頁網址的地方(Home/Index
)。但是,如果修改默認路由,如下所示,請注意,這將影響其他操作和控制器的URL。例如,如果您有一個名爲ExampleController
的控制器類和其中的一個操作方法,名爲DoSomething
,則預期默認網址ExampleController/DoSomething
將不再工作,因爲默認路由已更改。
對此的解決方法是不使用默認的路由很亂,創造了其他動作和控制器,像這樣的RouteConfig.cs文件新航線,
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Example",
url: "hey/now",
defaults: new { controller = "Example", action = "DoSomething", id = UrlParameter.Optional }
);
現在ExampleController
類的DoSomething
行動將被映射到網址hey/now
。但是,每次你想爲不同的動作定義路線時,這可能會很乏味。因此,在MVC 5您現在可以添加的屬性相匹配的網址,像這樣的行動,
public class HomeController : Controller
{
// url is now 'index/' instead of 'home/index'
[Route("index")]
public ActionResult Index()
{
return View();
}
// url is now 'create/new' instead of 'home/create'
[Route("create/new")]
public ActionResult Create()
{
return View();
}
}
在MVC 4,這可以在App_Start/RouteConfig.cs – NikolaiDante 2012-09-04 15:54:25
@NikolaiDante做你應該做的是評論一個答案,我幾乎錯過了它,它比這個答案更快。 :)謝謝 – GazB 2013-05-21 12:20:53