应用程序根目录的默认路由 [英] Default-Route for root of application

查看:156
本文介绍了应用程序根目录的默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何告诉我的 mvc 应用程序路由到特定的 Controller

How can I tell my mvc-application to route to a specific Controller and Action when they are not specified?

调试时 http:// localhost:54500 / 应该路由到 http:// localhost:54500 / Home / Index

当前我有:

routes.MapRoute(
    name: "Root",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
    );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

但这总是抛出


找不到视图索引或其主视图,或者没有视图引擎支持搜索到的位置

The view 'Index' or its master was not found or no view engine supports the searched locations

编辑#1

它应该重定向/路由到位于 Area 首页。只是想澄清一下,有一个 Controller 和一个 Area 都被命名为 Home

It should redirect/route to an View which resides in a Area called Home. Just want to clarify, that there is a Controller and an Area which both are named Home.

该区域的配置为:

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


推荐答案


在调试 http:// localhost:54500 / 时,应路由到 http:// localhost:54500 / Home / Index

实际上,您配置它的方式 http:// localhost:54500 / 将路由到 HomeController.Index 方法,而不是其他URL。

Actually, the way you have it configured, http://localhost:54500/ will route to the HomeController.Index method, not another URL.


未找到视图'Index'或其主视图或没有视图引擎支持搜索到的位置

The view 'Index' or its master was not found or no view engine supports the searched locations

此错误表示路由成功,但控制器返回的视图路径不正确存在。

This error indicates that routing succeeded, but the controller returned the path of a view that does not exist.

由于您还提到您正在使用区域并发布了配置,因此很清楚发生了什么。您的配置按以下顺序运行:

Since you also mentioned you are using an Area and have posted your configuration, it is clear what is happening. Your config is run in this order:

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

routes.MapRoute(
    name: "Root",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
    );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

因此,如果您传递URL http:// localhost:54500 / ,区域路线将丢失(因为它不是以 / Home 开头),并且会匹配 Root 路线。此 Root 路由未路由到您的区域。有两种方法可以解决此问题。

So, if you pass the URL http://localhost:54500/, the Area route will miss (because it doesn't start with /Home) and it will match the Root route. This Root route does not route to your Area. There are 2 ways to fix this.

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

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

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );



选项2-设置DataToken以指示本地区域



Option 2 - Set the DataToken to indicate the Home Area

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

routes.MapRoute(
    name: "Root",
    url: "",
    defaults: new { controller = "Home", action = "Index" }
    ).DataTokens["area"] = "Home";

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
    );

这篇关于应用程序根目录的默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆