Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在MVC5中添加路由_Asp.net Mvc_Asp.net Mvc 4_Asp.net Mvc 5 - Fatal编程技术网

Asp.net mvc 在MVC5中添加路由

Asp.net mvc 在MVC5中添加路由,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-5,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc 5,我有一个要求,我必须映射下面的url /amer/us/en/ = Home controller /amer/us/en/login/index = Home controller /amer/us/en/confirmation = Confirmation controller 以及常规的默认操作 例如,如果用户转到 http:\\test.com --> http://test/home/index http:\\test.com/amer/us/en/login/index

我有一个要求,我必须映射下面的url

/amer/us/en/ = Home controller
/amer/us/en/login/index = Home controller
/amer/us/en/confirmation = Confirmation controller
以及常规的默认操作

例如,如果用户转到

http:\\test.com --> http://test/home/index
 http:\\test.com/amer/us/en/login/index  --> http://test/home/index
 http:\\test.com/amer/us/en/   --> http://test/home/index
我正在研究属性路由,因此在HomeController中添加了以下代码

  [RoutePrefix("amer/us/en/")]
    [Route("{action=index}")]
    public class HomeController : Controller
    {

    }
我得到了这个错误
名为“Home”的控制器上的路由前缀“amer/us/en/”不能以正斜杠开始或结束
,而且默认路由现在不起作用,因此无法加载任何内容。下面是我的默认RouteConfig类

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

            routes.MapMvcAttributeRoutes();

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

对MVC来说非常陌生。有人能告诉我我做错了什么吗。

MVC中的路由通过在
RouteConfig
类中定义路由或通过属性路由(或者您可以使用区域)来工作。使用RouteConfig进行布线的顺序与您定义布线的顺序一致。当一个请求到来时,MVC将尝试从上到下的路由,并执行第一个它可以与请求的url匹配的路由。因此,您的示例中的路由需求可以通过以下方式实现:

routes.MapRoute(
            name: "RootLogin",
            url: "amer/us/en/login/index/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

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

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
这将把登录映射为一个特殊的路径,所有其他的
/amer/us/en/
路径都将转到它后面的
控制器和它的任何
操作。如果请求不是以
/amer/us/en
开头,则最后一条路由将执行默认行为


但是,看起来您希望将
/amer/us/en/
定义为一个区域,因此您可能也希望对此进行研究。

效果很好。非常感谢。