在控制器内设置动作&区域作为默认页面

时间:2013-08-27 11:22:32

标签: c# .net asp.net-mvc routes global-asax

我的路线配置中有以下地图路线。这是其他任何事情之前的第一条路线。

routes.MapRoute(
                "HomePage",
                "",
                new { area = "Accessibility", controller = "Cardholders", action = "Index" }
                );

然而,当我在浏览器中查看我的网站时,我得到了

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Cardholders/Index.aspx
~/Views/Cardholders/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Cardholders/Index.cshtml
~/Views/Cardholders/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

直接浏览我的操作http://localhost:54358/accessibility/cardholders/index

时,我没有任何问题

我想要实现的目标是输入http://localhost:54358并重定向到http://localhost:54358/accessibility/cardholders/index

根据以下答案,我试过

routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Cardholders", action = "Index", id = UrlParameter.Optional},
                // Parameter defaults
                new[] { "Plan.Web.Mvc.Areas.Accessibility.Controllers" }
                );

routes.MapRoute(
                "HomePage",
                "Accessibility_Default",
               "Accessibility/{controller}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
                );

routes.MapRoute(
                "HomePage",
                "Accessibility_Default",
               "Accessibility/{controller}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
            );

所有人似乎都无法工作。

4 个答案:

答案 0 :(得分:1)

我会尝试回答我自己的问题。

这是一种解决方法,我希望这会有所帮助。

我创建了一个名为“Home”的新区域,并在此区域内创建了一个名为“HomeController”的控制器。

在“Home”的区域注册中,我有这个

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute("", "", defaults: new { controller = "Home", action = "Index", area = "Home" });

    context.MapRoute(
        "Home_default",
        "Home/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

在我的HomeController中,我有这个

    public ActionResult Index()
    {
        return RedirectToAction("Index", "Cardholders", new { area = "Accessibility" });
    }

答案 1 :(得分:0)

尝试类似这样的事情

routes.MapRoute(
                "HomePage",
                "Accessibility_Default",
               "Accessibility/{controller}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
                );

希望这会对你有所帮助。

有关详细信息,请关注herehere

答案 2 :(得分:0)

试试这个

            routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "controller Name", action = "action name", id = UrlParameter.Optional }, // Parameter defaults
            new[] { "Your Area Controller Namespace" }
        );

答案 3 :(得分:0)

暂且放心,您只需要将MyApp替换为项目名称。

示例

routes.MapRoute(
    "HomePage",
    "",
    new { action = "Index" },
    new { controller = "Cardholders" },
    new[] { "MyApp.Areas.Accessibility.Controllers" }
);

当您进行测试时,将路线置于顶部,以便您知道没有其他路线干扰结果。