0

I want to change url in my page with the correct url after request in the controller by the attribute routing. How can i do please ?

By exemple, i have a correct url in the map route : localhost/blog/5/nameblog if i tape : localhost/blog/5/blabla, i want to diplay and rename in the correct url localhost/blog/5/nameblog once the blog id and his name have been found in database with id 5 :

[Route("{blog}/{Idblog}/{nameblog}")]
public ActionResult GetBlogById(int Idblog)
{
   // search the name blog with ID in database
   .....
   return View() // i want to display the correct url after the request
}

Thank you for your help !

Nkosi
  • 235,767
  • 35
  • 427
  • 472
laoho
  • 1
  • 1
  • 1
    Then what is the point of the last part of the URL, if you're just going to i̶g̶n̶o̶r̶e̶ correct it? –  Jun 22 '17 at 17:15
  • 2
    Similar to how the URL operates here on Stackoverflow? Then you need to take in the name of the blog as a parameter and then search for blog by id. if the provided blog name and searched blog name do not match then do a redirect or moved with the correct path. Done. I'll leave the rest as an exercise for you and you can get the details for the steps I described in many posts here already. And by the way you route template is wrong. `{blog}` should just be `blog`. ie `"blog/{Idblog}/{nameblog}"` – Nkosi Jun 22 '17 at 17:22
  • @Amy : I want juste display the right name of blog in url in my exemple, my goal is if the user type anything after son id localhost/blog/5/..., and if i found id 5 in database, the controller display the right name of blog in the last part of the url, I do not know if you understand me ! – laoho Jun 22 '17 at 17:23
  • 1
    I understand you completely. I explained above how you can do. and from a quick test I can confirm that stackoverflow does a 301 (Moved Permanently) when the name is excluded or does not match. Check the developer tool in your browser and you will see what I am talking about – Nkosi Jun 22 '17 at 17:28
  • We are not here to just do the work for you. Show some effort on your part and if you encounter problems then update question with relevant details so that we can provide you with assistance. – Nkosi Jun 22 '17 at 17:29
  • @Nkosi: thank you, i solve my problem with action filters – laoho Jun 22 '17 at 17:59
  • @laoho, That is great. Action filters were another way to go. Good call. You should add it as a self answer. I am sure there would be others interested in that – Nkosi Jun 22 '17 at 18:00

0 Answers0