簡體   English   中英

MVC 3區域注冊和視圖不起作用

[英]MVC 3 Area Registration and Views not working

我似乎在命名空間或注冊方面遇到問題。 在這一點上,我更傾向於MVC 3.0的區域和注冊問題,但是我願意接受這里的任何想法。

我的CAPAreaRegistration.cs文件看起來像這樣:

  namespace CISE.CINet.UserInterface.Areas.CAP
{
    public class CAPAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "CAP";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "CAP_default",
                "CAP/{controller}/{action}/{id}",
                new { action = "NewNomination", id = UrlParameter.Optional }
            );
        }
    }
}

我還嘗試添加我將所有內容更改為的名稱空間:

  public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "CAP_default",
                "CAP/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }, new string[] { "CISE.CINet.UserInterface.Areas.CAP.Controllers" }  // specify the new namespace 
            );
        }

Global.asax看起來像這樣:

    namespace CISE.CINet.UserInterface
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        #region PUBLIC FIELDS TO ALL APPLICATIONS

        //public static string SERVER_NAME = "ri-cinet-prd";
        public static string SERVER_NAME = "se-cinet-dev";
        public static string SHAREPOINT_PATH = "http://se-spoint-dev/";

        #endregion

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                "CAPLogin",
                "CAP/WelcomePartial",
                new { controller = "Home", action = "WelcomePartial", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }

家庭控制器工作正常,但是嘗試轉到CAP / NewNomination或CAP / Index無效嗎? 我收到404錯誤:

它直接指向NewNomination,但我也嘗試索引無濟於事

有任何想法嗎? 我的網址路徑不正確嗎?

嘗試將區域注冊類的名稱空間更改為與Global.asax.cs文件中的名稱空間相同。

我有同樣的問題,這至少對我來說是固定的。

我使用autofac作為DI容器,遇到了同樣的問題。 除了更改/注冊名稱空間外,還可以只注冊該區域的硬編碼(無論如何,啟動時的延遲最少):

var area = new MyAreaRegistration();
var rc = new AreaRegistrationContext(area.AreaName, RouteTable.Routes);
area.RegisterArea(rc);

或像這樣從您的容器中解決它們:

foreach (var area in container.Resolve<IEnumerable<AreaRegistration>>())
{
  var rc = new AreaRegistrationContext(area.AreaName, RouteTable.Routes);
   area.RegisterArea(rc);
}

然后在注冊中添加名稱空間即可解決該問題:

new[]{ "CISE.CINet.UserInterface.Areas.CAP.Controllers" }

暫無
暫無

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

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