asp.net mvc 中的区域,一次只工作一个 [英] Areas in asp.net mvc, only one work at time

查看:25
本文介绍了asp.net mvc 中的区域,一次只工作一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件夹如下所示:

  • (root)/Areas/Admin/Views/..
  • (root)/Areas/Admin/Controllers/...
  • (root)/Areas/Admin/Routes.cs

  • (root)/Areas/Admin/Views/..
  • (root)/Areas/Admin/Controllers/...
  • (root)/Areas/Admin/Routes.cs

(root)/Areas/Forum/Views/..

(root)/Areas/Forum/Views/..

(root)/Areas/Forum/Routes.cs

(root)/Areas/Forum/Routes.cs

public class Routes : AreaRegistration

{公共覆盖字符串 AreaName{得到 { 返回管理员";}}

{ public override string AreaName { get { return "Admin"; } }

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_Default",
        "{controller}/{action}/{Id}",
        new { controller = "Admin", action = "Index", Id = (string)null }
    );
}

}

公共类路由:AreaRegistration{公共覆盖字符串 AreaName{得到{返回论坛";}}

public class Routes : AreaRegistration { public override string AreaName { get { return "Forum"; } }

public override void RegisterArea(AreaRegistrationContext routes)
{
    routes.MapRoute(
        "Forum_Default",
        "{controller}/{action}",
        new { controller = "Forum", action = "Index"}
    );
}

}

Global.asax

Global.asax

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

        AreaRegistration.RegisterAllAreas();

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

起始页应该是 Home/Index 但它以 Admin/Index 开头,为什么?

The startpage should be Home/Index but it start with Admin/Index, why?

只有 site.com/Admin 有效,而不是 site.com/Forum

Only site.com/Admin works not site.com/Forum

我应该如何让管理和论坛区域正常工作?为什么只有管理员在工作,而不在论坛?

How should i get Admin and Forum Areas to work right? Why is only Admin working and not Forum?

当我删除 Admin/Routes.cs 文件时,论坛开始工作...

When i delete Admin/Routes.cs file Forum start to work...

在 ~/Views/中的主页即使我有

Home in ~/Views/ don't show as startpage even if i have

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

在我的 Global.asax 之后 AreaRegistration.RegisterAllAreas();

in my Global.asax after AreaRegistration.RegisterAllAreas();

推荐答案

我相信你的区域映射应该是这样结构的.

I believe your area mappings should be structured like so.

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_Default",
        "Admin/{controller}/{action}/{Id}",
        new { controller = "Admin", action = "Index", Id = (string)null }
    );
}

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Forum_Default",
        "Forum/{controller}/{action}/{Id}",
         new { controller = "Forum", action = "Index"}
    );
}

防止您的路线发生冲突,这就是我认为在您的情况下正在发生的事情.由于您的默认路由与您的管理路由相匹配.

Keeps your routes from conflicting, which is what i think is happening in your case. As your default route matches your admin route.

这篇关于asp.net mvc 中的区域,一次只工作一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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