Change URL After Action is Hit MVC?

2019-07-15 13:30发布

I want to change:

www.testurl.com/sports/blog/1 

Where sports is my area, blog is my action and 1 is an ID of a blog post, to:

www.testurl.com/sports/blog/test-title-of-blog

Where blog is still my action but the id is not shown, but instead the title/permalink of the blog is.

Here is my AreaRegistration for this action:

context.MapRoute(
                "sports",
                "sports/{action}/{content}",
                new { area = "Sports", controller = "Sports", action = "", content = "" });

Here is my action at the moment:

[HttpGet]
    public ActionResult Blog(string content)
    {
            int contentId;
            if (Int32.TryParse(content, out contentId))
            {
                model = service.GetBlogById(contentId);
            }
            else
            {
                model = service.GetBlogByTitle(content);
            }

            //Change URL to be: www.testurl.com/sports/blog/ + model.SEOFriendlyTitle
            return View(model);            
    }

Users are able to search via the ID of the blog, but also by the title of it, but I only want the title to appear in the url bar, never the id.

I cannot do this via Redirect rules due to the continuing maintenance that would cause.

  1. Is the controller the right place to do this? -Remember I may not have my title until after I retrieve it from the database using the ID

  2. How would I go about changing the URL to display the title vs. the ID?

2条回答
smile是对你的礼貌
2楼-- · 2019-07-15 14:00

I think what you should do is return a RedirectResult to the new Url if the ID is numeric and is a valid contentId :

            int contentId;
            if (Int32.TryParse(content, out contentId))
            {
                model = service.GetBlogById(contentId);
                if(model != null)
                {
                    return RedirectResult(/*url using the title*/);
                }
            }
            else
            {
                model = service.GetBlogByTitle(content);
            }

            //Change URL to be: www.testurl.com/sports/blog/ + model.SEOFriendlyTitle
            return View(model);

Of course, that will cause another round trip to the server but I can see a way to change the browser URL without a page redirect. You should also make sure that all published urls on your site are using the title instead of Id.

I hope it will help.

查看更多
爷、活的狠高调
3楼-- · 2019-07-15 14:00

I suggest giving this a quick read.

http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls

If you are really can't have the ID in Url and don't want to do redirects then I think storing Url as Slugs in the database is the only other option.

*Some points if you are going to do this.*

  1. Add a Unique Constraint to the column at the Database Level to avoid duplicates.

  2. Create a Database Index on this column to speed up you reads.

So with this Url

www.testurl.com/sports/blog/test-title-of-blog

This is your unique slug that you will query the database for instead of an ID

test-title-of-blog
查看更多
登录 后发表回答