[英]areas in asp.net mvc?
我在根區域有一個主頁路由:
routes.MapLocalizedRoute("HomePage",
"",
new { controller = "Home", action = "Index" },
new[] { "Nop.Web.Controllers" });
還有一個叫Xahoi的地區
namespace Nop.Web.Areas.Xahoi
{
public class XahoiAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Xahoi";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Xahoi_default",
"Xahoi/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "Nop.Web.Areas.Xahoi.Controllers" });
}
}
}
當我調用http://domain.com/xahoi/home- >時,它確實進入了Xahoi Area的Home控制器,但是它從根目錄中的Home控制器運行Index視圖。
另外,假設我要將呼叫更改為http://domain.com/xa-hoi ,我該怎么做?
要指定默認控制器,只需將其添加到第三個參數的數組中即可:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Xahoi_default",
"Xahoi/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Nop.Web.Areas.Xahoi.Controllers" });
}
顯示錯誤視圖的問題可能是第四個參數上的命名空間問題,並且它在錯誤的控制器中運行代碼...或者您的視圖文件夾結構的結構方式是無法找到視圖在Areas \\ Xahoi \\ Views \\ Home文件夾中,並且正在使用根視圖文件夾中的索引視圖。 也許您在Views文件夾中有一個索引視圖,而不是在Views \\ Home文件夾中?
要確定這是否是名稱空間問題,請在新控制器(該區域中的那個)Index方法上設置一個斷點,並查看在訪問頁面時是否被點擊。
希望這可以幫助
首先,我建議安裝一瞥 ,以更好地了解MVC如何在“幕后”工作。
基本上,它將在幾個地方查找控制器和視圖,使用瞥見將使這一點很明顯:)
在您的Xahoi地區,您是否實際上有一個名為Home的索引視圖? 例如Areas \\ Xahoi \\ Views \\ Home \\ Index.cshtml
另外,您可以在MapRoute中指定Area屬性,盡管我認為這是默認情況下的功能。 以下映射還應將您的網址更改為您要查詢的內容
公共重寫void RegisterArea(AreaRegistrationContext context){
context.MapRoute( "Xahoi_default", "xa-hoi/{controller}/{action}/{id}", new { Area=AreaName controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Nop.Web.Areas.Xahoi.Controllers" }); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.