如何在RegisterArea中编码MapRoute以将区域默认路由到另一个区域(网站根目录) [英] How to code MapRoute in RegisterArea to route the area default to another area (web site root)

查看:316
本文介绍了如何在RegisterArea中编码MapRoute以将区域默认路由到另一个区域(网站根目录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户直接访问区域根URL(\产品),我希望将其路由到网站主页(\Home)。

I want the user to be routed to the web site home page (\Home) if they access the area root url directly (\Products). What is the correct coding in the RegisterArea route.map?

这是项目中的文件夹结构,以及它们如何映射到网站

Here is the folder structure in the project and how they map to the web site

/区域/产品-> /产品

/Area/Products -> /Products

/ View / Home-> / Home

/View/Home - > /Home

如果用户转到/,则转到/ Home(对Home控制器使用index动作)

If users goes to / , then go to /Home (use the index action for controller Home)

如果用户转到/ Products,则转到/ Home(将索引动作用于控制器Home)

If users goes to /Products , then go to /Home (use the index action for controller Home)

如果用户转到/ Products / Fruit,则将index动作用于控制器Fruit; / Products / Fruit

If users goes to /Products/Fruit , then use the index action for controller Fruit; /Products/Fruit

我有一个标准的 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 = "Index", id = UrlParameter.Optional }
        );
    }

我在global.asax.cs文件中有一个标准条目

I have a standard entries in the global.asax.cs file

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

这是我更新的区域注册路线

Here is my updated area registration route

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

如果我转到产品,则会收到错误消息

If I go to \Products , I get error message

找不到资源。

描述:HTTP404。要查找的资源(或其依赖项之一)可能已被删除,更名或暂时不可用。请查看以下网址,并确保其拼写正确。

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

请求的URL:/ products /

Requested URL: /products/

有两种可能的解决方案,有人可能会在hack时考虑:

Two possible solutions, which some might consider at hack are:


  1. 在该区域中创建一个控制器,将默认值分配给该控制器,并将索引操作重定向回主控制器

  1. create a controller in the area, assign the default to that controller and redirect the index action back to the home controller

在Global.asax.cs文件中为/ products注册301重定向

register 301 redirect for /products in the Global.asax.cs file


推荐答案

已将ProductAreaRegistration配置为默认使用一个控制器,该控制器将重定向到网站根目录(area =)上的Home控制器。

The ProductAreaRegistration was configured to default to a controller that would redirect to the Home controller on the web site root (area="").

ProductAreaRegistration

    public class ProductAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "Product";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Product_default",
                "Product/{controller}/{action}/{id}",
                defaults: new { controller="Redirect", action = "Index", id = UrlParameter.Optional });
        }
    }

控制器

public class RedirectController : Controller
    {
        // GET: Product/Redirect
        public ActionResult Index()
        {
            return RedirectToAction("Index","Home",new { area = "" });
        }
    }

这篇关于如何在RegisterArea中编码MapRoute以将区域默认路由到另一个区域(网站根目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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