[英]MVC routing issue
Currently I'am working on an MVC project in which I try to get a kind of dynamic routing working. 目前,我正在一个MVC项目中,尝试在其中进行某种动态路由选择。 My idea would be that i left the original route in the global.asax.cs, so this one will take care of every controller I make. 我的想法是我将原始路由保留在global.asax.cs中,因此该路由将处理我制作的每个控制器。 For example the Contact and Account controllers. 例如,联系人和客户控制器。
Above controllers will have url's like 上面的控制器将具有url之类的
/Contact/
/Account/Logoff/ etc.
The second route I want to add is the one that is a kind of default route when there are no controllers found. 我要添加的第二条路由是找不到控制器时的一种默认路由。 In that case I assume this will be a route to a page or pagedetails. 在那种情况下,我认为这将是通往页面或pagedetails的路线。
Url's for example will be : 例如,网址为:
/BBQ/
/BBQ/Accesoires/
I have three routes added in the global.asax.cs which I think are correct. 我在global.asax.cs中添加了三个我认为正确的路由。 (Also in the correct order). (也以正确的顺序)。 Below I have added the routes: 在下面,我添加了路线:
routes.MapRoute(
"DefaultRoute", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Page", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"DefaultPageRoute",
"{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });
routes.MapRoute(
"SecondLevelPageRoute",
"{category}/{subCategory}",
new { controller = "Page", action = "PageDetails", category = UrlParameter.Optional, subCategory = UrlParameter.Optional });
with this setup the calls to the controllers work fine, but to the pages like /BBQ/ it gives below error: 通过这种设置,对控制器的调用工作正常,但是对/ BBQ /之类的页面却给出以下错误:
The resource cannot be found. 无法找到该资源。
If I comment the first route and go the the /BBQ/ url it works like a charm. 如果我注释掉第一条路线并转到/ BBQ /网址,它的工作原理就像一个魅力。 What am I overseeing in this routetable? 我在此路线表中负责什么?
You put the default route first, so it is trying to go to a route defined by {controller = "BBQ", Action = "Index" }
您将默认路由放在首位,因此它尝试转到由{controller = "BBQ", Action = "Index" }
定义的路由
That route should be the very last route. 那条路线应该是最后一条路线。 However, you need more detail in your routes. 但是,您需要在路线中提供更多详细信息。 Just having a category route will cause problems. 仅具有类别路线会引起问题。
For example, if this route is first 例如,如果这条路线是第一条
routes.MapRoute(
"DefaultPageRoute",
"{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });
Then a call to the URL /Contact/ will assume that you want to go to Page/Index/Contact
not /Contact/Index/{id}
. 然后,对URL / Contact /的调用将假定您要转到Page/Index/Contact
而不是/Contact/Index/{id}
。 I would use a more specific route that signifies that you are browsing a category like: 我将使用更具体的路由来表示您正在浏览以下类别:
routes.MapRoute(
"DefaultPageRoute",
"Category/{category}",
new { controller = "Page", action = "Index", category = UrlParameter.Optional });
So you will need to use a url www.mysite.com/Category/BBQ
to view what you want, but I don't think that's all bad. 因此,您将需要使用URL www.mysite.com/Category/BBQ
来查看所需的内容,但是我认为这并不很糟糕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.