在MVC asp.net中注册多个路由

编程入门 行业动态 更新时间:2024-10-18 20:26:02
本文介绍了在MVC asp中注册多个路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在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中注册多个路由

本文发布于:2023-11-14 04:20:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   路由   MVC   asp   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!