簡體   English   中英

Asp.NET MVC3 - Access區域中沒有區域的控制器

[英]Asp.NET MVC3 - Access Area's Controller Without Area in URL

我用ASP.NET MVC3 Framework創建了一個Web應用程序。 在項目中,我使用HomeController和Index操作添加名為Administrator的新區域。

它適用於http://localhost:2813/Administrator/Home/Index

但是當我嘗試使用URL http://localhost:2813/Home/Index

我收到一條錯誤消息:

'/'應用程序中的服務器錯誤。

未找到視圖“索引”或其主數據或視圖引擎不支持搜索的>>>位置。 搜索了以下位置:〜/ Views / Home / Index.aspx~ / Views / Home / Index.ascx~ / Views / Shared / Index.aspx~ / Views / Shared / Index.ascx~ / Views / Home / Index。 cshtml~ / Views / Home / Index.vbhtml~ / Views / Shared / Index.cshtml~ / Views / Shared / Index.vbhtml

描述:執行當前Web >>>請求期間發生未處理的異常。 請查看堆棧跟蹤以獲取有關錯誤的更多信息以及它在代碼中的起源位置。

我還在maproute中添加名稱空間,但它仍然不起作用。 有人有想法嗎? 謝謝。

更新問題:

這是Global.ascx routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Default", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Controllers" } ); MapRoute routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Default", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Controllers" } );

這是AdministratorAreaRegistration.ascx context.MapRoute( "Administrator", "Administrator/{controller}/{action}/{id}", new { area = "Administrator", controller = "Dashboard", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );

編輯
如果我將MapRoute從Global移動到AdministratorAreaRegistration(我無法理解並解釋它),問題就解決了
context.MapRoute( "Administrator", "Administrator/{controller}/{action}/{id}", new { area = "Administrator", controller = "Dashboard", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );
context.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults namespaces: new[] { "SampleWebsite.Controllers" } );

現在,對localhost/Home/Index所有請求都將是404找不到響應。 僅允許具有確切區域名稱(管理員)的請求。

謝謝你的幫助!

當您使用網址/Home/Index您不會通過該區域。 路由中使用Administrator前綴定義區域,並且URL中存在此前綴,允許路由引擎識別您在區域內。

有一些可能的解決方法來擁有默認區域。

如果您需要在區域內部啟動頁面,請在默認區域的HomeController中考慮這一點( /Controllers/HomeController.cs ):

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return RedirectToAction("Index", "Home",
            new { area = "Administrator" });
    }
}

感謝您添加路線。 如果你想要的是在url中沒有指定區域時使用你的管理區域,你可以嘗試用Global:asax替換你的路線:

routes.MapRoute( "Default", "{controller}/{action}/{id}", new { area = "Administrator", controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "SampleWebsite.Areas.Administrator.Controllers" } );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM