English 中文(简体)
自定义映像工具
原标题:Custom MapRoute

我试图建立一些自定义地图路线,

我的最终目的就是能够像下面那样具体地描述一些东西。 在哪些地方, 我基本上用“ id” 和“ name ” 的值对构建了自己的 URL 。 名称无关紧要, 仅对用户的闲暇而言, 我将要求在我的控制器中输入 ID 。

(dashboards/5-My-Estate-name-89-My分组-133-也许甚至一个分组-另一个分组)

首先,我在第一课工作,有麻烦。

浏览到“http://localhost:53933/dashboards/109-building-xyz”,同时使用以下路径产生错误A 公共行动方法109-building-xyz,在控制器我的Interface Interface.custators.Dashboards Consultants. 上找不到。

routes.MapRoute(
  "Dashboard",
  "dashboards/{id}-{name}", // URL pattern
   new { controller = "Dashboards", action = "Index" },
   new { id = @"d+", name = UrlParameter.Optional }
);

很明显,我希望这个方法 与索引函数的参数。

我做错什么了?我是否已经正确安排了?我来自网络-PHP背景,并使用Htaccess实现这些目的。

谢谢 谢谢

最佳回答

MVC将匹配第一个申报的路线,其模式将与URL模式相匹配。

所以,如果你有这个:

routes.MapRoute(
  "Default",
  "{controller}/{action}/{id}", // URL pattern
   new { controller = "Home", action = "Index" },
   new { id = UrlParameter.Optional }
);
routes.MapRoute(
  "Dashboard",
  "dashboards/{id}-{name}", // URL pattern
   new { controller = "Dashboards", action = "Index" },
   new { id = @"d+", name = UrlParameter.Optional }
);

網址http://localhost:53933/dashboards/109-building-xyz 将会发生什么情况,即MVC将匹配控制器的“shapboards”和“109-building-xyz”与动作的“shapboards”。

您需要总是先申报您最具体的路线,然后申报更一般的路线,例如:

routes.MapRoute(
  "Dashboard",
  "dashboards/{id}-{name}", // URL pattern
   new { controller = "Dashboards", action = "Index" },
   new { id = @"d+", name = UrlParameter.Optional }
);
routes.MapRoute(
  "Default",
  "{controller}/{action}/{id}", // URL pattern
   new { controller = "Home", action = "Index" },
   new { id = UrlParameter.Optional }
);

然而,Morten Mertner在他的评论中是对的-- 我不认为你可以有两个不被前斜线分隔的路线参数。 你需要将您的 URL 模式改变为类似模式, 才能使用默认路由引擎 :

"dashboards/{id}/{name}"
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签