Add a trailing slash at the end of each url?

后端 未结 6 669
傲寒
傲寒 2020-12-01 00:15

I have a little problem here. I need to add a trailing slash at the end of each url in the site I\'m working on. I defined all the links inside the site to have a trailing s

6条回答
  •  孤独总比滥情好
    2020-12-01 00:54

    The above solution by codingbug is very nice. I needed something very similar, but only for my root URL. I know there are possible problems with this, but here is what I did. It seems to work just fine in all of my environments. I think it works too, because it is only our Home page when they first come and do not have the training slash. That is the one case I was trying to avoid. If that is what you want to avoid, this will work for you.

      public class HomeController : Controller
      {
        public ActionResult Index()
        {
          if (!Request.RawUrl.Contains("Index") && !Request.RawUrl.EndsWith("/"))
            return RedirectToAction("Index", "Home", new { Area = "" });
    

    If it turns out I need more than this. I will probably use code that codingbug provided. Until then, I like to keep it simple.

    Note: I am counting on Home\Index to be removed from the URL by the routing engine.

提交回复
热议问题