1

I have looked through SO and the internet in General and I can't seem to understand whether the following is possible

I have a link to an action:

<a href = "@Url.Action("Subject", "Main", new { id = 1 })">Link to Subject</a>

This works fine, I can use the id, to get the Subject and all other info I need for the model.

In the browser it shows:

http://www.example.com/Main/Subject/1

This is just fine, but it is not pretty or very helpful for the user.

I would like to be able to look up the subject from the Id in the Action and modify the URL to look like:

http://www.example.com/Main/Subject/Science

Is this possible and if so, could someone give me a steer on where to find out how to do it.

I cannot change the parameter to a string because the subject name is not unique, only the id.

grayson
  • 945
  • 1
  • 11
  • 28
  • 1
    Its called a slug route. One way is per [this answer](http://stackoverflow.com/questions/30349412/how-to-implement-url-rewriting-similar-to-so/30363600#30363600) although that will generate `/Main/Subject/1/Science` (similar to what your seeing in the address bar now) –  Nov 09 '15 at 01:43
  • 1
    Is there a way to get `/Main/Subject/Science` without the 1? – Daniel Hoffmann-Mitscherling Nov 09 '15 at 02:30
  • 1
    @DanielHoffmann-Mitscherling, Not sure I like it, but `public ActionResult Subject(string ID) { int x; if (int.TryParse(ID, out x)) { string slug = ??; return RedirectToAction("Subject", new { ID = slug }); } return View(); }` should work. But a custom `FilterAttribute` would probably be better. –  Nov 09 '15 at 03:24
  • Ah I see, using an ActionResult to create a new request. Yes that would work well, was hoping something nicer in mvc routing would allow it. Thanks for the answer! – Daniel Hoffmann-Mitscherling Nov 09 '15 at 03:28
  • @StephenMuecke, thanks for this. This would work for me, but I want to not display the /1, just /SubjectName. Your solution in the second comment wont appear to work for me because Subject is not unique unless I use TempData to hold the Subject Id and then do the redirect that way. Or did I miss something. – grayson Nov 09 '15 at 21:28
  • @grayson, That why I indicated I did not like it :) –  Nov 09 '15 at 21:32

1 Answers1

0

well you can use the dBprovider feature in urlrewrite 2.0 forr iis 7 and above. what you have to do is create a table in database that store both urls i.e

 'http://www.example.com/Main/Subject/Science' 

and

  'http://www.example.com/Main/Subject/1' 

it will match with the url you want to show 'http://www.example.com/Main/Subject/Science' and redirect to the actual url i.e is to 'http://www.example.com/Main/Subject/1' for more info check this http://www.iis.net/learn/extensions/url-rewrite-module/using-custom-rewrite-providers-with-url-rewrite-module

Sujit.Warrier
  • 2,815
  • 2
  • 28
  • 47
  • Thank you, I had no idea this was possible. I'm not sure if this solution is right as I don't want to hit the db for every request when I only need it on 25% of the pages, but I will keep looking to see if there is a way to filter only the urls I want to look up. – grayson Nov 09 '15 at 21:29
  • it allows you to cache the fetched urls too – Sujit.Warrier Nov 10 '15 at 03:23
  • and it works. oh yeah to get db provider option iin your url rewrite module download rewrite extensibility from here http://www.microsoft.com/en-in/download/details.aspx?id=43353 – Sujit.Warrier Nov 10 '15 at 03:26