Asp.net Mvc地区路由问题

时间:2013-07-16 15:28:14

标签: c# asp.net-mvc asp.net-mvc-areas

我希望网站的默认页面是Login.cshtml。我得到了例外:

  

错误:未找到视图“LogIn”或其主控,或者没有视图引擎支持搜索的位置。搜索了以下位置:

我有两个方面。结构如下所示。

enter image description here

我的routeconfig如下所示。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Portal.Web
{
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 = "LogIn", id =   UrlParameter.Optional },
            namespaces: new[] { "Portal.Web.Areas.Management" }
        );
    }
    }

}

我的global.asax.cs如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

namespace Portal.Web
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
}
}

`你有什么建议吗?

4 个答案:

答案 0 :(得分:7)

你忘了一些东西

回顾:

ManagementAreaRegistration.cs

public class ManagementAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Management";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Management_default",
            "Management/{controller}/{action}/{id}",
          new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        "Default" // Route name
        , "{controller}/{action}/{id}" // URL with parameters
        , new { area = "management", controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        , new[] { "Portal.Web.Areas.Management.Controllers" } // Namespace of controllers in root area
    );
}   

您设置Portal.Web.Areas.Management时应该Portal.Web.Areas.Management.Controllers它还缺少默认区域:area = "management"

答案 1 :(得分:1)

看起来您需要在MapRoute上更改名称空间:

Portal.Web.Areas.Management

要:

Portal.Web.Areas.Management.Controllers

答案 2 :(得分:-1)

清除bin文件夹并重建。旧路由有可能设置旧的dll。这对我来说至少适用于一条路线。

答案 3 :(得分:-5)

我按照这篇文章中的说明进行操作。问题解决了。访问ASP.NET MVC Default URL View

谢谢大家。