Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make this ASP.NET MVC Route for a twitter-style URL work?
    primarykey
    data
    text
    <p>trying to map the following style of route: <a href="http://site.com/username" rel="nofollow noreferrer">http://site.com/username</a> in the same way that you can do <a href="http://www.twitter.com/user" rel="nofollow noreferrer">http://www.twitter.com/user</a></p> <p>My initial solution was to have these routes:</p> <pre><code>//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 ); </code></pre> <p>And that was working fine, until I tried to do the following in my 'Links' controller:</p> <pre><code>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); } </code></pre> <p>These RedirectToAction methods try and return the browser to <a href="http://site.com/Users" rel="nofollow noreferrer">http://site.com/Users</a> (I do have a users controller) instead of directing to <a href="http://site.com/Links/index" rel="nofollow noreferrer">http://site.com/Links/index</a></p> <p><strong>Why is this is happening?</strong></p> <p><strong>How should I be organizing my routes to make this work properly?</strong></p> <p>I'm happy sacrificing <a href="http://site.com/links" rel="nofollow noreferrer">http://site.com/links</a> and moving to <a href="http://site.com/links/index" rel="nofollow noreferrer">http://site.com/links/index</a> if I have to. But how would I enforce that?</p> <p>Thanks to all for any help</p> <p><strong>EDIT:</strong></p> <p>I know what's causing this, it's trying to redirect to <a href="http://site.com/links" rel="nofollow noreferrer">http://site.com/links</a> (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).</p> <p><strong>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?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload