计算器像URL路径 [英] StackOverflow like URL Routing

查看:152
本文介绍了计算器像URL路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它我的理解是在StackOverflow的问题有以下格式

  http://stackoverflow.com/questions/ {问题-ID} / {塞制造 - 从 - 问题 - 标题}

所以基本上,问题是使用的问题-ID检索。所以无论价值我给蛞蝓是无关紧要的。

首先,我想知道这个理解是否是错误的:)

我有一个网址

<$p$p><$c$c>http://stackoverflow.com/questions/6291678/convert-input-string-to-a-clean-readable-and-browser-acceptable-route-data

然后,我改变了塞手动这个样子。

  http://stackoverflow.com/questions/6291678/naveen

但变更为原来的蛞蝓。萤火虫给我一个permenant上改变URL重定向301。如何实现这个功能?


解决方案

您可以用的Response.RedirectPermanent 做到这一点,因为ASP.NET 4.0提供:

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$psponse.redirectpermanent.aspx\">http://msdn.microsoft.com/en-us/library/system.web.htt$p$psponse.redirectpermanent.aspx

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!的IsPostBack)
    {
        。字符串ID = RouteData.Values​​ [ID]的ToString();
        。字符串passedSlug = RouteData.Values​​ [名称]的ToString();
        //从数据库/ dymanic方法的原始蛞蝓
        字符串originalSlug = GetSlugFromID(ID);        如果(!originalSlug.Equals(passedSlug))
        {
            VAR URL =的String.Format(〜/测试/ {0} / {1},身份证,originalSlug);
            的Response.RedirectPermanent(URL,真);
        }
    }
}

在一个不相关的旁注,想客户所想Stack Overflow是不保存在数据库中的蛞蝓。它从标题<动态创建href=\"http://stackoverflow.com/questions/6291678/convert-input-string-to-a-clean-readable-and-browser-acceptable-route-data/6291788#6291788\">using像这样。我只是改变了我的问题的标题和蛞蝓改变。这将是未需要存储在数据库中的蛞蝓,因为它是多余的标题

Its my understanding that the questions in StackOverflow has the following format

http://stackoverflow.com/questions/{question-id}/{slug-made-from-question-title}

So basically the question is retrieved using the question-id. so whatever value I give the slug is immaterial.

First I would like to know whether this understanding is wrong :)

I have a URL

http://stackoverflow.com/questions/6291678/convert-input-string-to-a-clean-readable-and-browser-acceptable-route-data

Then I changed the slug manually like this.

http://stackoverflow.com/questions/6291678/naveen

But it changed to the original slug. Firebug showed me a permenant redirect 301 on the altered URL. How to implement this functionality?

解决方案

You can do this with Response.RedirectPermanent available since ASP.NET 4.0:

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.redirectpermanent.aspx

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        string id = RouteData.Values["id"].ToString();
        string passedSlug = RouteData.Values["name"].ToString();
        //get the original slug from database / dymanic method
        string originalSlug = GetSlugFromID(id);

        if(!originalSlug.Equals(passedSlug))
        {
            var url = String.Format("~/test/{0}/{1}", id, originalSlug);
            Response.RedirectPermanent(url, true);
        }
    }
}

On an unrelated side note, would like to think Stack Overflow is not saving the slug at database. Its created dynamically from title using something like this. I just altered the title of my question and the slug changed. It will be un-necessary to store the slug in database as it is redundant to title.

这篇关于计算器像URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆