在MVC asp.net中注册多个路由 [英] Registering Multiple Routes in MVC asp.net

查看:129
本文介绍了在MVC asp.net中注册多个路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MVC中映射多个具有不同顺序参数的路由:

I want to map several routes in MVC that have the parameters in different orders:

localhost:1010/abcd/home/index
localhost:1010/home/index/abcd

id=abcd
controller=home
action=index

我尝试了下面的代码,但没有用.

I tried the code below, but it doesn't work.

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

    routes.MapRoute(
        "ShoppingManagment",
        "{id}/{controller}/{action}",
        new { controller = "ShoppingManagment",
            action = "ShoppingManagment", id = UrlParameter.Optional });


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

推荐答案

由于两个路由具有相同的格式,因此无法使用.

It will not work because both routes have the same format.

因此MVC路由引擎无法区分这两种网址格式.

So the MVC Routing Engine cannot differentiate between both the url patterns.

尝试将Controller直接写入url模式.

Try writing the Controller directly into the url pattern.

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

        routes.MapRoute(
          "ShoppingManagment",
          "{id}/ShoppingManagment/{action}",
          new { controller="ShoppingManagment", action = "ShoppingManagment", id = UrlParameter.Optional });


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

    }

这篇关于在MVC asp.net中注册多个路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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