Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that you didn't tell MVC to expect more than one dash; so it grabbed what it found and tried to parse it that way.</p> <p>Well, that just sucks for us. </p> <p>If you know that your route will always be:</p> <pre><code>{book}-{name}-{perhapsMoreName-{maybeEvenMore} </code></pre> <p>Then we can handle that, but it's going to take work on the server side to parse that.</p> <p>Believe me, it'd be much easier just to use slashes. If someone comes to you and complains about SEO, ask them to show you the exact place where it matters (And somehow why slashes don't work as well). I'm not saying they're wrong, but there's a lot of, "I think it works this way with SEO" and that gets annoying, fast.</p> <p>I'm going to provide you with two solutions. Solution 1 would be the way I would handle it (and fight for) and solution 2 would be the solution to your particular problem with your current scheme for handling (and I've dealt with solution 2 in practice, and <strong>at best</strong> it is <em>very</em> icky). </p> <h3>Solution 1</h3> <p>Set up your routes similar to how Stack Overflow sets up theirs. This would allow your SEO guy to have his hallowed SEO, and you would get what you want: No ulcers.</p> <p>Your URLs would change to look like: </p> <p><code>http://example.com/books/id/book-type-name</code></p> <pre><code>routes.MapRoute("bookRoute", "books/{id}/{*bookName}", new { controller = "Notes", action = Show, id = "", bookName = "" } ); </code></pre> <p>Then, you can use the bookName for display, but not actually use it to get any code. Just change your <code>Show</code> method to retrieve items by their Id.</p> <h3>Solution 2</h3> <p>This solution uses a ActionFilter to intercept the requests and parse out what's what. It's an icky solution, but it should work.</p> <p>I <a href="https://stackoverflow.com/questions/6760157/how-does-routebase-getroutedata-work-or-pointers-for-implementing-pattern-matchin/6778267#6778267">wrote an answer that showed this solution</a> for another question. It is applicable here as well, just change the search type to query the database based on what's in the URL. Also, the route will have to go at the bottom of your routes, otherwise it'll catch that first. All the standard disclaimers apply: Use parameretized queries, or an ORM that parameteretizes them for you.</p>
 

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