需要MVC3路由帮助 [英] MVC3 Routing Help Required

查看:75
本文介绍了需要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 });


似乎合理:您想先检查指定讨论的网址,因为它们与标准"和用户"模式都匹配.我们不能将用户"路由放在默认"之前,因为它与任何以http://localhost:4341 /Controller/格式传递的URL匹配.但是,同样,默认路由将与http://localhost:4341 /Username/匹配,将 username 解释为控制器,(并默认尝试访问其Index操作) .您可能会限制网址的各个部分(请参见 http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs [用户名"也可以使事情变得简单得多.

希望这对&很清楚:似乎很难解释!


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 http://localhost:4341/Controller/ . But, equally the default route will match http://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 http://www.asp.net/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路由帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆