需要MVC3路由帮助

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

我正在MVC3中进行应用程序. IO已实现以下功能:如果用户在URL后输入其用户名,则将其重定向到配置文件页面 例如,如果他输入http:\ localhost:4341 \ username 那么他将被重定向到http:\ localhost:4341 \ users \ showprofile \ username 如果输入的用户名无效,则将其重定向到找不到页面http:\ localhost:4341 \ users \ pagenotfound 这工作正常,但是如果用户键入任何控制器的名称,他也将重定向到未找到的页面,在这种情况下,我想将他重定向到控制器的索引页面 例如,如果他输入了Discussioncontroller的名称http:\ localhost:4341 \ Discussion 他应该重定向到http:\ localhost:4341 \ Discussion 我该如何实施? 这是我的路线

Hi, I am making an application in MVC3. IO have implemented the functionality that if user type after URL his/her username then he''ll be redirected to the profile page for eg if he types http:\localhost:4341\username then he''ll be redirected to http:\localhost:4341\users\showprofile\username and if he entered an invalid username he''ll be redirected to page not found http:\localhost:4341\users\pagenotfound This is working fine, however if a user types the name of any controller the he is also redirecting to pagenot found, i want to redirect him to the controller''s index page in this case for eg if he entered Discussioncontroller''s name http:\localhost:4341\Discussion he should be redirected to http:\localhost:4341\Discussion How can i implement this? This is my route

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

在家庭索引上,我已经做出了类似的条件

and on Home''s Index i have make a condition like

if (id != null) return RedirectToAction("ShowProfile", "User", new { @id = id }); return View();

推荐答案

您当前的路由策略是: 1.在URL 中仅接受一个可选的ID "{id}", 2.它始终默认为Home控制器,索引动作. 只要满足一项条件,任何条件都将与该条件匹配,您的应用程序认为http:\ localhost:4341 \ Discussion中的讨论是用户名,因为它匹配. 解决方法: Your current routing strategy is: 1. It takes an optional Id only "{id}", in the url 2. It always defaults to Home controller, Index action. Anything will match this condition as long as it has one item, you app thinks that discussion in http:\localhost:4341\Discussion is a username because it matches. To fix : routes.MapRoute( "Discussion", "Discussion/{id}", new { controller = "Home", action = "Discussion", id = UrlParameter.Optional } //or whatever you need); routes.MapRoute( "Default", "{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

映射的顺序很重要,请颠倒这些顺序,它将在到达预期的映射之前将"Discussion"作为用户名进行匹配. 但是在相当大的应用程序中,我会对此策略保持警惕.假设您仍然想要默认的"{controller}/{action}/{id}"和默认的controller(例如Home)和action(例如Index).第一个刺可能是:

The order of the Mappings is important, reverse these and it will match "Discussion" as a username before it reaches the mapping intended for it. But I''d be wary of this strategy on a reasonably large application. Assuming you still want the default "{controller}/{action}/{id}" with defaults on controller(e.g. Home), and action (e.g. Index). The first stab might be:

routes.MapRoute( "Discussion", "Discussion/{id}", new { controller = "Home", action = "Discussion", id = UrlParameter.Optional } //or whatever you need); routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "index", id = UrlParameter.Optional } //or whatever you need); routes.MapRoute( "User", "{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

这似乎合理:您想先检查指定讨论的网址,因为它们与标准"和用户"模式都匹配.我们不能将用户"路由放在默认"之前,因为它与任何以localhost:4341 /Controller/格式传递的URL匹配.但是,同样,默认路由将与localhost:4341 /Username/匹配,将 username 解释为控制器,(并默认尝试访问其Index操作) .您可能会限制网址的各个部分(请参见 www.asp/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs [ ^ ])来执行您想要的操作,但是您可能需要考虑一下需要而是根据您的要求,仅将用户名传递到URL中.即使只是更改为"/Users/用户名"也可以使事情变得简单得多. 希望这对&很清楚:似乎很难解释!

This seems reasonable: you want to check for urls specifying the discussion first, as they''d match both the "standard" and "user" patterns. We cannot put the "User" route before the "Default" as it matches any URL passed in the format localhost:4341/Controller/ . But, equally the default route will match localhost:4341/Username/ , interpreting username as a controller, (and attempting to access its Index action by default). You can potentially constrain the parts of the url (see www.asp/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs[^]) to do what you want, but you may want to think about the need to pass just the username into the URL instead, depending upon your requirements. Even just changing to "/Users/username" could make things much easier. Hope this helps & is clear: it seems difficult to explain!

更多推荐

需要MVC3路由帮助

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

发布评论

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

>www.elefans.com

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