首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使URL在MVC 5搜索引擎优化友好和一致?

如何使URL在MVC 5搜索引擎优化友好和一致?
EN

Stack Overflow用户
提问于 2016-01-05 15:39:55
回答 1查看 2.2K关注 0票数 3

我试图使我的网站urls更友好的搜索引擎优化,所以我使用这种方法来改变路由。(虽然我不知道这种方法是最好的还是不-但这不是我的问题!)

代码语言:javascript
复制
public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "interface",
                url: "soal/{id}/{slug}",    /* "soal" is only decor */
                defaults:new { controller = "ui", action = "singleQuestion",
                    slug = UrlParameter.Optional}    /*I made "slug" optional because I don't need this part to retrieve conternt from database */
                                      /* slug only explains content of the webpage*/

                );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

这是处理页面的"SingleQuestion“操作:

代码语言:javascript
复制
        public ActionResult singleQuestion(int id, string slug) 
                                                   /* "slug" parameter here does nothing. Action Does not use this.*/
    {
        var questions = BQcnt.Questions;
        var showQuestion = questions.Find(id);
        if (showQuestion != null)
        {
            var AnswerOfShowQuestion = BQcnt.Answers.Where(
                i => i.QuestionID == showQuestion.QuestionID);

            ViewBag.lastQuestion = showQuestion;
            ViewBag.answer_of_show_question = AnswerOfShowQuestion;
        }
        return View(questions.OrderByDescending(x => x.QuestionID));
    }

当我使用这些Urls时,这个操作很好:

代码语言:javascript
复制
http://localhost:9408/soal/13/bla-bla-bla

代码语言:javascript
复制
http://localhost:9408/soal/13

这些URL直接指向相同的页面,但我唯一的问题是:当我使用第二个url时,我希望当页面加载自动的url更改以完成一个url,并在url中添加段塞,就像第一个.。

编辑:我想要一些和stackoverflow.com urls完全一样的东西。这两个网址:

代码语言:javascript
复制
http://stackoverflow.com/questions/34615538/how-to-make-url-in-mvc-5-seo-friendly-and-consistent

http://stackoverflow.com/questions/34615538

打开相同的页面,但是当我们使用第二个页面(您可以检查)时,url会自动转换为第一个页面。但我的urls不是这样翻译的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-06 16:17:04

好的,这个问题是我自己解决的。

代码语言:javascript
复制
public ActionResult singleQuestion(int id, string slug)
{
    var questions = BQcnt.Questions;
    var showQuestion = questions.Find(id);
    if (showQuestion != null)
    {
        var AnswerOfShowQuestion = BQcnt.Answers.Where(
            i => i.QuestionID == showQuestion.QuestionID);

        ViewBag.lastQuestion = showQuestion;
        ViewBag.answer_of_show_question = AnswerOfShowQuestion;
        if (slug != showQuestion.slug)
        {

            return RedirectToAction("singleQuestion", new { id = id, slug = showQuestion.slug });
        }
        else
        {
            return View(questions.OrderByDescending(x => x.QuestionID));
        }
    }
    return RedirectToAction("Index");
}

也许是肮脏的..。但是完美的工作..。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34615538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档