如何使这个支持Twitter风格的URL的ASP.NET MVC路由工作?

时间:2021-03-22 04:05:53

trying to map the following style of route: http://site.com/username in the same way that you can do http://www.twitter.com/user

尝试映射以下样式的路由:http://site.com/username,就像您可以使用http://www.twitter.com/user一样

My initial solution was to have these routes:

我最初的解决方案是拥有这些路线:

//site.com/rathboma - maps to user details for rathboma
            routes.MapRoute("Users", "{id}", new { controller = "Users", action = "Details" });

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "oneday" }  // Parameter defaults
            );

And that was working fine, until I tried to do the following in my 'Links' controller:

这工作正常,直到我尝试在我的'Links'控制器中执行以下操作:

public ActionResult Details(string id)
        {
            int newId;
            if (!int.TryParse(id, out newId))
                return RedirectToAction("Index");


            WebLink results = Service.GetWebLink(newId, 5);

            if (results == null)
                return RedirectToAction("Index");

            return View(results);
        }

These RedirectToAction methods try and return the browser to http://site.com/Users (I do have a users controller) instead of directing to http://site.com/Links/index

这些RedirectToAction方法尝试将浏览器返回到http://site.com/Users(我有一个用户控制器),而不是直接访问http://site.com/Links/index

Why is this is happening?

为什么会这样?

How should I be organizing my routes to make this work properly?

我该如何组织我的路线以使其正常工作?

I'm happy sacrificing http://site.com/links and moving to http://site.com/links/index if I have to. But how would I enforce that?

如果必须的话,我很高兴牺牲http://site.com/links并转到http://site.com/links/index。但是我该如何执行呢?

Thanks to all for any help

感谢所有人的帮助

EDIT:

I know what's causing this, it's trying to redirect to http://site.com/links (the index page), but links is being picked up as a username and redirecting to /users/details, when it can't find the user 'links' it tries to redirect to the UsersController Index action which maps to /users, and the cycle continues ('users' is not a user it can find so redirects infinately).

我知道造成这种情况的原因是,它正在尝试重定向到http://site.com/links(索引页面),但链接被选为用户名并重定向到/ users / details,当它找不到时用户'链接'它尝试重定向到映射到/ users的UsersController索引操作,并且循环继续('用户'不是它可以找到的用户,因此无限重定向)。

So I guess My sub-question is: how to I make mvc always use /links/index instead of just using /links for the index page?

所以我想我的子问题是:如何让mvc始终使用/ links / index而不是仅使用/ links作为索引页面?

2 个解决方案

#1


Try adding this route before your Users route:

尝试在用户路由之前添加此路由:

routes.MapRoute("Links", 
                "{controller}/{id}", 
                 new { controller = "Links", action = "Details" });

This should then work for

这应该适用于

http://mysite.com/Links/id

&

http://mysite.com/username

#2


I believe changing RedirectToAction("Index"); to RedirectToAction("Index", "Links"); in your Links controller should solve the issue without having to change your routes.

我相信改变RedirectToAction(“索引”);到RedirectToAction(“索引”,“链接”);您的Links控制器应解决问题,而无需更改您的路由。

The problem is you have two very greedy routes. What I'd do is to break apart the default route to less greedy routes like this :

问题是你有两条非常贪婪的路线。我要做的是将默认路由拆分为不那么贪婪的路由,如下所示:

routes.MapRoute("Links", 
                "Links/{id}", 
                 new { controller = "Links", action = "Index" });


routes.MapRoute("Users", 
                "{id}", 
                 new { controller = "Users", action = "Details" });

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

Causing the urls to be like the following :

使网址如下:

site.com/links/5  - hits the Links controller
site.com/name     - hits the Users controller
site.com/         - hits the home controller

#1


Try adding this route before your Users route:

尝试在用户路由之前添加此路由:

routes.MapRoute("Links", 
                "{controller}/{id}", 
                 new { controller = "Links", action = "Details" });

This should then work for

这应该适用于

http://mysite.com/Links/id

&

http://mysite.com/username

#2


I believe changing RedirectToAction("Index"); to RedirectToAction("Index", "Links"); in your Links controller should solve the issue without having to change your routes.

我相信改变RedirectToAction(“索引”);到RedirectToAction(“索引”,“链接”);您的Links控制器应解决问题,而无需更改您的路由。

The problem is you have two very greedy routes. What I'd do is to break apart the default route to less greedy routes like this :

问题是你有两条非常贪婪的路线。我要做的是将默认路由拆分为不那么贪婪的路由,如下所示:

routes.MapRoute("Links", 
                "Links/{id}", 
                 new { controller = "Links", action = "Index" });


routes.MapRoute("Users", 
                "{id}", 
                 new { controller = "Users", action = "Details" });

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

Causing the urls to be like the following :

使网址如下:

site.com/links/5  - hits the Links controller
site.com/name     - hits the Users controller
site.com/         - hits the home controller